Unity3D Script KeynoteII

Unity3D Script KeynoteII

1、使用代码操作Particle。

 1     //粒子对象
 2     GameObject particle = null;
 3     //粒子X轴方向速度
 4     float velocity_x = 0.0f;
 5     //粒子Y轴方向速度
 6     float velocity_y = 0.0f;
 7     //粒子Z轴方向速度
 8     float velocity_z = 0.0f;
 9     
10     void Start () 
11     {
12         //获得粒子对象
13         particle = GameObject.Find("ParticleSystem");
14         
15     }
16     
17     void OnGUI()
18     {
19         //拖动设置粒子的最大尺寸
20         GUILayout.Label("粒子最大尺寸");
21         particle.particleEmitter.maxSize = GUILayout.HorizontalSlider (particle.particleEmitter.maxSize, 0.0f, 10.0f,GUILayout.Width(150));
22         
23         //拖动设置粒子的最大消失时间
24         GUILayout.Label("粒子消失时间");
25         particle.particleEmitter.maxEnergy = GUILayout.HorizontalSlider (particle.particleEmitter.maxEnergy, 0.0f, 10.0f,GUILayout.Width(150));
26         
27         //拖动设置粒子的最大生成数量
28         GUILayout.Label("粒子的最大生成数量");
29         particle.particleEmitter.maxEmission = GUILayout.HorizontalSlider (particle.particleEmitter.maxEmission, 0.0f, 100.0f,GUILayout.Width(150));
30         
31         //拖动设置粒子X轴的移动速度
32         GUILayout.Label("粒子x轴的移动速度");
33         velocity_x= GUILayout.HorizontalSlider (velocity_x, 0.0f, 10.0f,GUILayout.Width(150));
34         particle.particleEmitter.worldVelocity = new Vector3(velocity_x, particle.particleEmitter.worldVelocity.y, particle.particleEmitter.worldVelocity.z);
35         
36         //拖动设置粒子Y轴的移动速度
37         GUILayout.Label("粒子y轴的移动速度");
38         velocity_y= GUILayout.HorizontalSlider (velocity_y, 0.0f, 10.0f,GUILayout.Width(150));
39         particle.particleEmitter.worldVelocity = new Vector3( particle.particleEmitter.worldVelocity.x,velocity_y, particle.particleEmitter.worldVelocity.z);
40         
41         //拖动设置粒子Z轴的移动速度
42         GUILayout.Label("粒子z轴的移动速度");
43         velocity_z= GUILayout.HorizontalSlider (velocity_z, 0.0f, 10.0f,GUILayout.Width(150));
44         particle.particleEmitter.worldVelocity = new Vector3( particle.particleEmitter.worldVelocity.x, particle.particleEmitter.worldVelocity.y,velocity_z);
45     
46     
47     }
View Code

2、布料是Unity3.x引入的特色组件,是柔软的,可以变成任意形状,比如随风飘扬的旗子呈窗户上的窗帘。

3、Trail Renderer

  The Trail Renderer is used to make trails behind objects in the scene as they move about.

  可以通过以下类似代码来操作TrailRender:

 1     //路径渲染对象
 2     private TrailRenderer trialRender;
 3     
 4     void Start () 
 5     {
 6         //获取路径渲染对象
 7         trialRender = gameObject.GetComponent<TrailRenderer>();
 8     }
 9 
10     void OnGUI()
11     {
12         
13         if(GUILayout.Button("增加宽度",GUILayout.Height(50)))
14         {
15             trialRender.startWidth +=1;
16             trialRender.endWidth   +=1;
17         }
18         
19         if(GUILayout.Button("显示路径",GUILayout.Height(50)))
20         {
21             trialRender.enabled = true;
22         }
23         
24         if(GUILayout.Button("隐藏路径",GUILayout.Height(50)))
25         {
26             trialRender.enabled = false;
27         }
28     }
View Code

Input

1、判断按键是否按下。

 1         if (Input.GetKeyDown (KeyCode.W))
 2         {
 3             Debug.Log("您按下了W键");
 4         }
 5         
 6         if (Input.GetKeyDown (KeyCode.S))
 7         {
 8             Debug.Log("您按下了S键");
 9         }
10         
11         if (Input.GetKeyDown (KeyCode.A))
12         {
13             Debug.Log("您按下了A键");
14         }
15         
16         if (Input.GetKeyDown (KeyCode.D))
17         {
18             Debug.Log("您按下了D键");
19         }
20         
21         if (Input.GetKeyDown (KeyCode.Space))
22         {
23             Debug.Log("您按下了空格键");
24         }
View Code

2、判断按键是否抬起。

 1         //抬起按键
 2         if (Input.GetKeyUp (KeyCode.W))
 3         {
 4             Debug.Log("您抬起了W键");
 5         }
 6         
 7         if (Input.GetKeyUp (KeyCode.S))
 8         {
 9             Debug.Log("您抬起了S键");
10         }
11         
12         if (Input.GetKeyUp (KeyCode.A))
13         {
14             Debug.Log("您抬起了A键");
15         }
16         
17         if (Input.GetKeyUp (KeyCode.D))
18         {
19             Debug.Log("您抬起了D键");
20         }
21         
22         if (Input.GetKeyUp (KeyCode.Space))
23         {
24             Debug.Log("您抬起了空格键");
25         }
View Code

 3、长按事件。使用Input.GetKey()方法判断键盘中某个键是否一直处于按下状态

 1         if (Input.GetKeyDown (KeyCode.A))
 2         {
 3             Debug.Log("A按下一次");
 4         }    
 5         if (Input.GetKey (KeyCode.A))
 6         {
 7             //记录按下的帧数
 8             keyFrame++;
 9             Debug.Log("A连按:" + keyFrame+"");
10         }
11         if (Input.GetKeyUp (KeyCode.A))
12         {
13             //抬起后清空帧数
14             keyFrame=0;
15             Debug.Log("A按键抬起");
16         }    
View Code

 4、使用Input.anyKeyDown() 来判断任意键是否按下,Input.anyKey()判断任意键是否被长按。

 1     // Update is called once per frame
 2     void Update ()
 3     {
 4         if(Input.anyKeyDown)
 5         {
 6             //清空按下帧数
 7             keyFrame=0;
 8             Debug.Log("任意键被按下");
 9         }
10         
11         
12         if(Input.anyKey)
13         {
14             keyFrame++;
15             Debug.Log("任意键被长按"+keyFrame+"");
16         }
17     
18     }
View Code

5、鼠标按下事件。

 1     void Update ()
 2     {
 3         
 4         if (Input.GetMouseButtonDown(0))
 5         {
 6             Debug.Log("点击鼠标左键的位置为:" +Input.mousePosition);
 7         }
 8         if (Input.GetMouseButtonDown(1))
 9         {
10             Debug.Log("点击鼠标右键的位置为:" +Input.mousePosition);
11         }
12         if (Input.GetMouseButtonDown(2))
13         {
14             Debug.Log("点击鼠标中键的位置为:" +Input.mousePosition);
15         }
16         
17     }
View Code

6、使用Input.GetMouseButtonUp()方法可以监听鼠标抬起。

 1     void Update () 
 2     {
 3     
 4         if (Input.GetMouseButtonUp(0))
 5         {
 6             Debug.Log("鼠标抬起左键的位置为:" +Input.mousePosition);
 7         }
 8         if (Input.GetMouseButtonUp(1))
 9         {
10             Debug.Log("鼠标抬起右键的位置为:" +Input.mousePosition);
11         }
12         if (Input.GetMouseButtonUp(2))
13         {
14             Debug.Log("鼠标抬起中键的位置为:" +Input.mousePosition);
15         }
16     
17     }
View Code

7、Input.GetMouseButton()用于监听鼠标长按事件。

 1         //连按事件
 2         if(Input.GetMouseButton(0))
 3         {    
 4             MouseFrame++;
 5             Debug.Log("鼠标左键长按"+MouseFrame+"");
 6         }
 7         if(Input.GetMouseButton(1))
 8         {
 9             MouseFrame++;
10             Debug.Log("鼠标右键长按"+MouseFrame+"");        
11         }
12         if(Input.GetMouseButton(2))
13         {
14             MouseFrame++;
15             Debug.Log("鼠标中键长按"+MouseFrame+"");            
16         }
View Code

 8、使用Input.GetAxis()获取某个自定义按键的轴值。

1     void Update () 
2     {
3         float value = Input.GetAxis ("test"); 
4         Debug.Log("按键轴的数值为:"+value);
5     }
View Code

9、字符串转换为int。

  

Application

1、Application.LoadLevel()可以切换场景。

  

2、Application.CaptureScreenShot()可以截图。

   

3、Application.OpenURL()可以打开网页。

4、Application.Quit()退出游戏。

AssetDataBase

1、使用AssetDataBase.LoadAssetAtPath()可以设置行为。

  

2、使用AssetDataBase.CreateAsset()可以创建资源。

  

3、AssetDataBase.CreateFolder()可以创建目录。 

4、AssetDataBase.CopyAsset()可以移动资源,AssetDataBase.MoveAsset()可以移动资源。

5、AssetDataBase.DeleteAsset()、AssetDataBase.Reresh()。

原文地址:https://www.cnblogs.com/tekkaman/p/3537637.html