Unity脚本基础
2015-07-29 18:52
525 查看
Unity脚本基础
①事件函数
Unity事件函数using UnityEngine; using System.Collections; public class Demo1 : MonoBehaviour { //只执行一次 void Start() { Debug.Log("Demo1.cs 类中的Start() 运行"); } //执行多次 void Update () { Debug.Log("Demo1.cs 类中的Update() 运行"); } }
②GameObject类
学习创建、克隆、销毁对象。using UnityEngine; using System.Collections; public class Demo : MonoBehaviour { GameObject goCloneObj; //克隆的对象 void Start (){ //创建游戏对象 GameObject goCreatObj=GameObject.CreatePrimitive(PrimitiveType.Cube); //添加材质。 goCreatObj.renderer.material.color = Color.red; //添加名称 goCreatObj.name = "CubeName"; //克隆对象 goCloneObj=(GameObject)GameObject.Instantiate(goCreatObj); } void Update () { //销毁对象 if(Input.GetKey(KeyCode.D)) { //GameObject.Destroy(goCloneObj); //没有参数,表示立即销毁 GameObject.Destroy(goCloneObj,2F); //2F 表示2秒后延迟销毁。 } } }
using UnityEngine; using System.Collections; public class Demo3 : MonoBehaviour { void Update () { //动态增加一个脚本 if(Input.GetKeyDown(KeyCode.A)) { this.gameObject.AddComponent("Demo4_SelfRotation"); } } }
自旋转脚本:
using UnityEngine; using System.Collections; public class Demo4_SelfRotation : MonoBehaviour { void Update () { this.transform.Rotate(Vector3.up,Space.World); } }
using UnityEngine; using System.Collections; public class Demo5_GetCompont : MonoBehaviour { void Start () { //使用GetComponet() 方法,做类之间的数值传递。 int intValue=this.gameObject.GetComponent<Demo6_TestObj>().GetValues(); string strReturn=this.gameObject.GetComponent<Demo6_TestObj>().Str1; Debug.Log("得到Demo6脚本组件中的字符串是:" + strReturn); Debug.Log("得到Demo6脚本组件中的数值是:" + intValue); } }
using UnityEngine; using System.Collections; public class Demo6_TestObj : MonoBehaviour { public string Str1 = "大家好!"; public int GetValues() { return 10; } }
③学习MonoBehaviour 类
using UnityEngine; using System.Collections; public class Demo7_Invoke : MonoBehaviour { void Start (){ //调用函数 Invoke("DisplayInfo", 3F); //"3F" 表示延迟调用时间 //重复调用函数 //参数含义: //"5F" 表示延迟调用时间(单位:秒) //"1F" 表示重复调用函数的间隔时间 InvokeRepeating("DisplayInfo_2",5F,1F); } void DisplayInfo(){ Debug.Log("DisplayInfo() 方法被执行了。"); } void DisplayInfo_2(){ Debug.Log("DisplayInfo_2() 方法被执行了。"); } }
④学习Transform 类
using UnityEngine; using System.Collections; public class Demo8_Transform : MonoBehaviour { void Start () { //调整当前游戏对象的位置、旋转、缩放 this.transform.position = new Vector3(2F,3F,4F); this.transform.eulerAngles = new Vector3(45F,0F,0F); this.transform.localScale = new Vector3(3F,3F,3F); } }
⑤学习层级视图的游戏对象查找技术(属于GameObject)
using UnityEngine; using System.Collections; public class Demo9 : MonoBehaviour { void Start () { //按照“名称”基本查找(游戏对象) GameObject goObj1=GameObject.Find("Cube_1"); goObj1.renderer.material.color = Color.blue; //按照“别名”,查找游戏对象。 GameObject goObj2=GameObject.FindWithTag("SphereTag"); goObj2.renderer.material.color = Color.red; //按照“别名”,查找游戏对象组。 GameObject[] goObjArray = GameObject.FindGameObjectsWithTag("CapsuleTag"); foreach (GameObject Goitem in goObjArray) { Goitem.renderer.material.color = Color.yellow; } } }
⑥学习Time
using UnityEngine; using System.Collections; public class Demo10 : MonoBehaviour { //"Update" 事件函数特点: //单位时间内运行的次数是不确定的,与计算机的性能和具体分配的资源有很大关系。 void Update() { print("Time.delteTime"+Time.deltaTime); //表示“帧”的间隔时间。 if (Input.GetKey(KeyCode.W)) { this.transform.Translate(Vector3.forward * Time.deltaTime * 50, Space.World); } else if (Input.GetKey(KeyCode.S)) { this.transform.Translate(Vector3.back * Time.deltaTime * 50, Space.World); } } }
相关文章推荐
- Unity 优化全解析2
- Unity 优化全解析1
- 问题解决:Ubuntu 12.04在vmware10下无法进入Unity模式
- GameUnity 2.0 发布倒计时
- UNITY3D学习笔记7
- Unity如何挂接Android程序
- Unity 物理碰撞检测
- Unity3D的Socket通信
- Unity3D之NGUI说明书(转)
- Unity3D之使用ITween制作自动漫游超简单
- Unity3d 中的Mathf 运算
- Unity Sprite Atlas Compression
- Unity第一人称和第三人称视角脚本
- 基于Unity5.0系统的新打包方式
- DoTween(HOTween V2) 教程
- VS2013无法调试Unity3D问题
- 【Unity】代码加密(二)dll加密so加密
- Unity5中Inspector界面上的AssetBundle值设定问题
- [MarsZ]Unity3d游戏开发之Unity3d全策划配置新手指引
- [MarsZ]Unity3d游戏开发之Unity3d全策划配置新手指引