Unity3D笔记 英保通三 脚本编写 、物体间通信
一、脚本编写
1.1、同一类型的方法JS和C#的书写方式却不一样主要还是语法,在工程中创建一个Cube 分别把JSTest.js和CSharp.cs 添加到Cube中
JSTest.js
#pragma strict private var i:int; private var f:float; function Start () { } function Update () { } function SetInt(_i:int) { i=_i; } function SetFloat(_f:float) { f=_f; } function GetInt():int { return i; } function GetFloat():float { return f; }
CSharp.cs
using UnityEngine; using System.Collections; public class CSharpTest : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { } int i; float f; public void SetInt(int i) { this.i = i; } public void SetFloat(float f) { this.f = f; } public int GetInt() { return i; } public float GetFloat() { return f; } }
1.2、JS,C#调用方法赋给Main Camera
MainJS.js
#pragma strict var obj:GameObject; function Start () { obj=GameObject.Find("Cube"); var js:JSTest=obj.GetComponent(JSTest); js.SetInt(1); js.SetFloat(1.1); print("JS GetInt :"+js.GetInt()); print("JS GetFloat:"+js.GetFloat()); } function Update () { }
MainCharp.cs
using UnityEngine; using System.Collections; public class MainCSharp : MonoBehaviour { GameObject obj; // Use this for initialization void Start () { obj = GameObject.Find ("Cube"); CSharpTest cs = obj.GetComponent<CSharpTest> (); cs.SetInt (11); cs.SetFloat (11.11f); Debug.Log ("C# GetInt "+cs.GetInt()); Debug.Log ("C# GetFloat "+cs.GetFloat ()); } // Update is called once per frame void Update () { } }
1.3、看看结果
结果有点意外:JS应该先执行,为什么出来的顺序是这个?
"Standard Assets"、 "Pro Standard Assets" 和 "Plugins" 这三个目录里的脚本被最先编译,"Editor"目录里的稍后编译,其他的脚本最后编译。
二、物体间通信
2.1、
GameObject.SendMessage:向自身的脚本中发送消息
GameObject.BroadcastMessage:向自身及子物体的脚本中发送消息
GameObject.SendMessageUpwards:向自身及父物体中发送消息(先发给自己在发送给其他物体)
例如:子类Sphere对应脚本sendMessage.cs 父类Cube对应脚本receiveMessage.cs
sendMessage.cs
using UnityEngine; using System.Collections; public class sendMessage : MonoBehaviour { // Use this for initialization void Start () { gameObject.SendMessageUpwards("SendMsgParent", "Hello 来自子类Sphere"); } // Update is called once per frame void Update () { } void SendMsgParent(string msg) { Debug.Log("我是子类Sphere,子类发来的消息:" + msg); } }
receiveMessage.cs
using UnityEngine; using System.Collections; public class receiveMessage : MonoBehaviour { // Use this for initialization void Start() { } // Update is called once per frame void Update() { } void SendMsgParent(string msg) { Debug.Log("我是父类Cube,子类发来的消息:" + msg); } }
最终结果
发送消息:先发送给自己在发送给其他物体
2.2、上面有个缺陷就是必须是继承关系 方可传递消息
解决方案:可以用C# 委托和事件机制来改变,可以实现不同物体间的进行消息传递
步骤:新建两个GameObject :EventCube、ListenCube 做事件和监听。对应分别的cs文件EventCube.cs、ListenCube.cs
EventCube.cs
using UnityEngine; using System.Collections; public class EventCube : MonoBehaviour { public delegate void EventHandle(GameObject e); public event EventHandle MouseOver; void OnMouseOver() { if (MouseOver != null) { MouseOver(gameObject); } } // Use this for initialization void Start () { } // Update is called once per frame void Update () { } }
ListenCube.cs
using UnityEngine; using System.Collections; public class ListenCube : MonoBehaviour { // Use this for initialization void Start() { EventCube ev = GameObject.Find("EventCube").GetComponent<EventCube>();//EventCube 事件物体 ev.MouseOver += new EventCube.EventHandle(ev_MouseOver);//监听函数 //ev.MouseOver += ev_MouseOver; } void ev_MouseOver(GameObject e) { this.transform.Rotate(20, 0, 0); Debug.Log("旋转 :" + e); //throw new System.NotImplementedException(); } void Listening(GameObject e) { } // Update is called once per frame void Update() { } }
效果:
转载于:https://www.cnblogs.com/PEPE/p/3590053.html
- Unity3D笔记 英保通三 脚本编写 、物体间通信
- unity3d脚本学习笔记2
- selenium+python脚本编写笔记(十)
- Unity3D中角色的动画脚本的编写(一)
- shell时间,shell创建文件,linux shell编写以日期时间为文件名的脚本学习笔记 (转)
- Unity3D计算角色与物体之间的距离来触发动画【脚本】
- Unity3D中C#编写脚本
- Unity3D 学习笔记(二)脚本的使用
- pyton 编写脚本检测两台主机之间的通信状态,异常邮件通知
- Shell编写脚本的笔记
- selenium+python脚本编写笔记(四)
- 大数据学习笔记:编写脚本分发配置,数据分布,以及使用代码打印调试信息
- Unity学习笔记——利用脚本实现对一个物体的第三人称观察
- Unity3D【脚本】把一个ui物体,对准世界坐标中的物体,可以实现例如血条等
- unity3D C#脚本实现鼠标拖动物体
- Unity3D自学笔记——脚本开发中的3D数学
- 使用Lua脚本为wireshark编写自定义通信协议解析器插件
- Unity3D笔记九 发送广播与消息、利用脚本控制游戏
- shell脚本编写小例子——工作笔记
- selenium+python脚本编写笔记(五)