当前位置:首页 » 《随便一记》 » 正文

unity 3d实例:创建游戏对象、旋转的立方体、Unity3D Button、图片按钮、Box控件、Label控件、Background Color、Color_秃头橘子的博客

21 人参与  2022年05月28日 17:40  分类 : 《随便一记》  评论

点击全文阅读


实例:创建游戏对象、旋转的立方体、Unity3D Button、图片按钮、Box控件、Label控件、Background Color、Color

1.创建游戏对象
C#方法

using UnityEngine;
using System.Collections;
public class CreatePrimitive:MonoBehaviour {
	void OnGUI () {
		if(GUILayout.Button("CreateCube",GUILayout.Height(50))){
        GameObject m_cube=GameObject.CreatePrimitive(PrimitiveType.Cube);
            m_cube.AddComponent<Rigidbody>();
            m_cube.GetComponent<Renderer>().material.color=Color.blue;
            m_cube.transform.position=new Vector3(0,10,0);
        }
    
if(GUILayout.Button("CreateSphere",GUILayout.Height(50))){
        GameObject m_cube=GameObject.CreatePrimitive(PrimitiveType.Sphere);
            m_cube.AddComponent<Rigidbody>();
            m_cube.GetComponent<Renderer>().material.color=Color.red;
            m_cube.transform.position=new Vector3(0,10,0);
        }
    }
}

2.旋转的立方体
C#方法

using System.Collections;
using UnityEngine;

public class Myscript : MonoBehaviour {

    public GameObject myCube;
    public int transSpeed = 100;
    public float rotaSpeed = 10.5f;
    public float scale = 3;
    void OnGUI() {
        if (GUILayout.Button("移动立方体")) {
            myCube.transform.Translate(Vector3.forward * transSpeed * Time.deltaTime, Space.World);
        }
        if(GUILayout.Button("旋转立方体")){
            myCube.transform.Rotate(Vector3.up*rotaSpeed,Space.World);
        }
        if(GUILayout.Button("缩放立方体")){
            myCube.transform.localScale=new Vector3(scale,scale,scale);
        }
    }
}

3.Unity3D Button
js方法

function OnGUI() {
    if (GUI.Button(Rect(0, 0, 100, 50), "click")) {
        print("you have click here!");
    }
}

4.图片按钮
js方法

var btnTexture:Texture;
var atnTexture:Texture;
function OnGUI()
{
if(!btnTexture)
{
Debug.LogError("Please assign a texture on the inspector");
return;
}
if(!atnTexture)
{
Debug.LogError("Please assign a texture on the inspector");
return;
}
if(GUI.Button(Rect(Screen.width/2-50,Screen.height/2+130,70,70),atnTexture))
{
UnityEngine.SceneManagement.SceneManager.LoadScene("scene1");
}
if(GUI.Button(Rect(Screen.width/2+30,Screen.height/2+130,70,70),btnTexture))
{
UnityEngine.SceneManagement.SceneManager.LoadScene("scene2");
}}

5.Box控件
js方法


function OnGUI(){
GUI.Box(Rect(0,0,100,50),"Top-left");
GUI.Box(Rect(Screen.width -100,0,100,50),"Top-Righ");
GUI.Box(Rect(0,Screen.height -50,100,50),"Buttom-Left");
GUI.Box(Rect(Screen.width -100,Screen.height -50,100,50),"Buttom-Right");

}

6.Label控件
js方法

var textureToDisplay:Texture2D;
function OnGUI(){
GUI.Label(Rect(10,10,100,10),"hello!!");
GUI.Label(Rect(10,40,textureToDisplay.width,textureToDisplay.height),textureToDisplay);
}

7.Background Color
js方法

function OnGUI() {
    GUI.backgroundColor = Color.red;
    GUI.Button(Rect(10, 110, 70, 30), "A button");
}

8.Color
js方法

public static var color:Color;
function OnGUI(){
GUI.color=Color.yellow;
GUI.Label(Rect(10,10,100,20),"Hello world");
GUI.Box(Rect(10,50,50,50),"A BOX");
GUI.Button(Rect(10,110,70,30),"A button");
}

都是一些小操作而已,因为今天总结了,所以就记录一下下,不打算深入学习unity


点击全文阅读


本文链接:http://zhangshiyu.com/post/40963.html

立方体  方法  控件  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 她的记忆停留在了最爱初恋的那年许欣柔楚临川完本_她的记忆停留在了最爱初恋的那年(许欣柔楚临川)
  • 全书浏览假千金的实习生男友霸占我办公室,我反手让他们倾家荡产(顾家明)_假千金的实习生男友霸占我办公室,我反手让他们倾家荡产(顾家明)全书结局
  • 童养夫让我给他的新欢出修复费(林嘉芝林思雅)_童养夫让我给他的新欢出修复费林嘉芝林思雅
  • 全文资助生女婿让我给他白月光付三千万月子中心钱(宋清玉宋雅)列表_全文资助生女婿让我给他白月光付三千万月子中心钱
  • 碎在时光里的谎言喻景宴秦明月完本_碎在时光里的谎言(喻景宴秦明月)
  • 旧爱剜心吻成灰席鄢之岑秋全书免费旧爱剜心吻成灰席鄢之岑秋全书免费
  • 结婚六年丈夫不碰我谁知儿子亲爹是寡头(纪清言傅司砚),结婚六年丈夫不碰我谁知儿子亲爹是寡头
  • 老公想换掉我的男胎,我笑他自不量力(宋薇于继业)_老公想换掉我的男胎,我笑他自不量力宋薇于继业
  • 给太子下了噬心蛊后,皇后找上门(小夭赵劼)全书浏览_给太子下了噬心蛊后,皇后找上门全书浏览
  • 豪门绝嗣!带球跑的夫人回来了!(谢长宴慕清杳)_豪门绝嗣!带球跑的夫人回来了!谢长宴慕清杳
  • 完美身材(李朵林之晴)_完美身材李朵林之晴
  • 离婚后,我和快穿系统绑定(白意秋陈荣周立慧)_离婚后,我和快穿系统绑定(白意秋陈荣周立慧)

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1