unity3d常用的游戏对象引用技巧
2016-05-26 15:42
543 查看
常用引用技巧
1. 当要获取某类标签名为“car”的游戏对象:
privateGameObject[] cars;//声明汽车游戏对象数组
cars = GameObject.FindGameObjectsWithTag("car");//找到Tag为car的所有游戏对象
foreach(GameObject car
in cars) {//遍历汽车数组
car.transform.RotateAround(Vector3.up,Time.deltaTime
*speed);//让所有的汽车绕Y轴自转
}
2. 全局变量容器使用PlayerPrefs:
PlayerPrefs.SetInt("music",musicIndex%2 + 1);//设置music
if(PlayerPrefs.GetInt("music")
!= 1 &&!GetComponent<AudioSource>().isPlaying) {//当music值不为2,且音乐没有播放
GetComponent<Audi
4000
oSource>().Play();//播放音乐
}
3. 限制鼠标在某一个区域运动,关联屏幕的坐标,与摄像头无关:
4. using UnityEngine;
5. usingSystem.Collections;
6.
7. publicclassTest :
MonoBehaviour {
8.
9. // Use this for initialization
10. publicGameObject test;
11. privatefloat horizR, vertR;//当前屏幕与默认屏幕宽、高比
12. void Start () {
13. test.transform.position =
newVector3(0,0,1);//限制区域z=1
14.
15. }
16.
17. // Update is called once per frame
18. void Update () {
19. horizR =
Input.mousePosition.x /
Screen.width;
20. vertR =
Input.mousePosition.y /
Screen.height;
21. test.transform.position =
newVector3(horizR *100-50,vertR * 50-25,1);//区域长度为100,宽度为50,因此区域的左下角坐标为(-50,-25,
1)
22.
23. }
24. }
欢迎大家评论和交流!
1. 当要获取某类标签名为“car”的游戏对象:
privateGameObject[] cars;//声明汽车游戏对象数组
cars = GameObject.FindGameObjectsWithTag("car");//找到Tag为car的所有游戏对象
foreach(GameObject car
in cars) {//遍历汽车数组
car.transform.RotateAround(Vector3.up,Time.deltaTime
*speed);//让所有的汽车绕Y轴自转
}
2. 全局变量容器使用PlayerPrefs:
PlayerPrefs.SetInt("music",musicIndex%2 + 1);//设置music
if(PlayerPrefs.GetInt("music")
!= 1 &&!GetComponent<AudioSource>().isPlaying) {//当music值不为2,且音乐没有播放
GetComponent<Audi
4000
oSource>().Play();//播放音乐
}
3. 限制鼠标在某一个区域运动,关联屏幕的坐标,与摄像头无关:
4. using UnityEngine;
5. usingSystem.Collections;
6.
7. publicclassTest :
MonoBehaviour {
8.
9. // Use this for initialization
10. publicGameObject test;
11. privatefloat horizR, vertR;//当前屏幕与默认屏幕宽、高比
12. void Start () {
13. test.transform.position =
newVector3(0,0,1);//限制区域z=1
14.
15. }
16.
17. // Update is called once per frame
18. void Update () {
19. horizR =
Input.mousePosition.x /
Screen.width;
20. vertR =
Input.mousePosition.y /
Screen.height;
21. test.transform.position =
newVector3(horizR *100-50,vertR * 50-25,1);//区域长度为100,宽度为50,因此区域的左下角坐标为(-50,-25,
1)
22.
23. }
24. }
欢迎大家评论和交流!
相关文章推荐
- Unity3D中的线性插值Lerp()函数解析
- UNITY3D自学(五)-- 导航寻路的学习
- unity中如何解决两个物体的绑定
- Unity 2D射线与 3D射线 UI射线
- Unity5.x的GUIText被UI Text所取代--增加头文件UnityEngine.UI
- Unity3D客户端和Java服务端使用Protobuf
- unity ugui自适应
- unity 优化 WaitForTargetFPS Gfx.WaitForPresent Graphics.PresentAndSync
- Unity3D 物体移动方式总结
- unity3d 各种路径&权限
- Unity实用案例之——CameraController
- Unity3D实现3D立体游戏原理及过程
- unity3D 涂涂乐使用shader实现上色效果
- unity3D AR涂涂乐制作浅谈
- 【Unity3D】游戏研发团队及岗位职责
- Unity Android/IOS 打开图片库和相机,并加载图片
- Unity3D 查找Update函数体为空的类
- 【Unity3D】Unity3D工具、Mono工具、内部脚本工作原理以及跨平台特性
- Unity3D 渲染统计窗口Stats
- 猫都能学会的Unity3D Shader入门指南