unity 基本函数整理[4_Unity脚本 + 5_游戏元素的设计]

Update()   每一帧执行一次
LateUpdate() 在Update后执行,同样也是每帧执行一次
Awake()    在Start()函数前执行,用以初始化
FixedUpdate()   固定更新
Start()    初始化
OnDestroy()  销毁对象 -->  Destroy(this.gameObject);
OnGUI()      每一帧都调用




创建游戏对象:

using UnityEngine;
using System.Collections;

public class CreatObject : MonoBehaviour {
	GameObject obj;

	void OnGUI(){
		if(GUILayout.Button("create Cube",GUILayout.Height(20))){
			obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
			//obj.AddComponent(Rigidbody);
			obj.name = "yoho";
			obj.renderer.material.color = Color.blue;
			obj.transform.position = new Vector3(0,0,0);
		}

	}
}

获取游戏对象:

GameObject obj = GameObject.Find("Cube");
GameObject obj = GameObject.FindGameObjectWithTag("plants");

旋转


GameObject obj = GameObject.FindGameObjectWithTag("plants");

obj.transform.Rotate (Vector3.up * Time.deltaTime * 5);  
obj.transform.Rotate (new Vector3(0,1,0));  
obj.transform.Rotate (0.0f,Time.deltaTime * 200, 0.0f);

通过Tags获取游戏对象
新建Tags “plants”,并将标签赋予给游戏对象,

using UnityEngine;
using System.Collections;

public class FindGameObject : MonoBehaviour {
	GameObject[] objs = null;

	void Start () {
		objs = GameObject.FindGameObjectsWithTag ("plants");
	}

	void OnGUI(){
		if (GUI.Button (new Rect (100, 50, 150, 30), "change material")) {
			objs [0].renderer.material.color = Color.blue;
			objs [1].renderer.material.color = Color.red;	
		}


	}
}
判断标签是否为TestTag:

if(obj.tag == "TestTag"){}

判断标签是否包含TestTag:

if(obj.CompareTag("TestTag")){}

添加组件
添加刚体组件
<pre name="code" class="csharp">//正确:
big.AddComponent<Rigidbody>();
//错误:	
big.AddComponent (Rigidbody);



using UnityEngine;
using System.Collections;


public class MyAddComponent : MonoBehaviour {
GameObject obj = null;
// Use this for initialization
void Start () {
obj = GameObject.Find ("Cube");


}

// Update is called once per frame
void Update () {

}
void OnGUI(){
if (GUI.Button (new Rect (150, 30, 150, 30), "add Component")) {
obj.AddComponent ("FindGameObject");
}
}
}

 



脚本之间消息的传送机制:广播机制




产生克隆对象  [Instantiate]


using UnityEngine;
using System.Collections;

public class MyAddComponent : MonoBehaviour {
	GameObject obj = null;
	float i = 1.0f;
	Vector3 pos;

	void Start () {
		obj = GameObject.Find ("Sphere");
		pos =obj.transform.position;
	}

	void OnGUI(){
		if (GUI.Button (new Rect (150, 30, 150, 30), "add Component")) {
			pos.x += i;
			GameObject clone = (GameObject)Instantiate(obj,pos,Quaternion.identity);	
			Destroy(clone,5);
		}
	}
}

删除组件

Destroy(obj.GetComponent<FindGameObject>());

GameObject的变换:

obj.transform.position = new Vector3(x,y,z);

//移动
//this.transform.Translate (Vector3.forward * Time.deltaTime);  
//this.transform.Translate (Vector3.right * Time.deltaTime);  
//this.transform.Translate (Vector3.up * Time.deltaTime); 


//旋转
//this.transform.Rotate (Vector3.up * Time.deltaTime * 5);  
//this.transform.Rotate (new Vector3(0,1,0));  

sma.transform.RotateAround (big.transform.position,Vector3.forward,Time.deltaTime * 25);


//缩放:
obj.transform.localScale = Vector3(scaleX,scaleY,scaleZ);


主线程等待:
//略...




随机数:

Random.Range(0,  100);
Random.Range(0.0f,  100.0f);

得到对象的尺寸

float size_X = plane.GetComponent<MeshFilter>().mesh.bounds.size.x;
float size_Y = plane.GetComponent<MeshFilter>().mesh.bounds.size.y;
float size_Z = plane.GetComponent<MeshFilter>().mesh.bounds.size.z;

float scale_X =  plane.transform.localScale.x;

maxWidth = scale_X * size_X;


【5_游戏元素的设计】



I、地形的设计
Create -> Terrain


地形的设置共有7个按钮:
依次为:
1、设置地形高度,(按住shift建则减小高度)
2、设置地行动同等高度
3、润化地形元素,使地形圆滑、
4、画笔,设置地形的纹理
5、设置树木的贴图,以及在地形上栽种树木
6、设置草坪的贴图,以及在地形上种植草坪
7、对地形进行整体上的设置




II、灯光的设计


//略....




III、SkyBoxs




IV、摄像机
Camera设置参数:
Clear Flags:设置背景的显示内容
Culling Mask:设置某些层是否显示
View Rect: 设置摄像机在游戏界面的显示范围






6、物理引擎


Component ->  Physics   ->   Rigidbody
Is Kinematic: 
选中,则表示物体的运动依靠脚本进行运动
不选,表示物体所有的运动依靠物理引擎模拟实现




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值