您的位置:首页 > 移动开发 > Unity3D

Unity3D笔记 英保通三 脚本编写 、物体间通信

2019-07-11 10:21 1026 查看
原文链接:http://www.cnblogs.com/PEPE/p/3590053.html

一、脚本编写

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: