Unity3D笔记九 发送广播与消息、利用脚本控制游戏

一、发送广播与消息

  游戏对象之间发送的广播与消息分为三种:第一种向子对象发送,将发送至该对象的同辈对象或者子孙对象中;第二种为给自己发送,发送至自己本身对象;第三种为向父对象发送,发送至该对象的同辈或者父辈对象中;

using UnityEngine;
using System.Collections;

public class _4_3 : MonoBehaviour {

    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {
        gameObject.BroadcastMessage("ReceiveBroadcastMessage", "A0-BroadcastMessage()");//向子类发送消息
        gameObject.SendMessage("ReceiveSendMessage", "A0-SendMessage()");//给自己发送消息
        gameObject.SendMessageUpwards("ReceiveSendMessageUpwards", "A0-ReceiveSendMessageUpwards()");//向父类发送消息
    }

    //接收父类发送的消息
    void ReceiveBroadcastMessage(string str)
    {
        Debug.Log("A0----Receive:"+str);
    }
    //接收自己发送的消息
    void ReceiveSendMessage(string str)
    {
        Debug.Log("A0----Receive:" + str);
    }
    //接收子类发送的消息
    void ReceiveSendMessageUpwards(string str)
    {
        Debug.Log("A0----Receive:" + str);
    }
}   

二、游戏对象克隆

  使用Instantiate();方法来克隆游戏对象

using UnityEngine;
using System.Collections;

public class _4_4 : MonoBehaviour
{

    GameObject obj;
    // Use this for initialization
    void Start()
    {
        obj = GameObject.Find("Sphere");
    }

    void OnGUI()
    {
        if (GUILayout.Button("克隆游戏", GUILayout.Width(100), GUILayout.Height(50)))
        {
            Object o = Instantiate(obj, obj.transform.position, obj.transform.rotation);//[ɪns'tænʃɪeɪt]
            Destroy(o, 5);//5秒后销毁该实例
        }
    }
    // Update is called once per frame
    void Update()
    {

    }
}

三、旋转游戏对象

两种:第一种为自身旋转,意思是模型沿着自己的x轴、y轴或z轴方向旋转;第二种为围绕旋转,意思是模型围绕着坐标系中的某一点或某一个游戏对象整体来做旋转。

   transform.Rotate():该方法用于设置模型绕自身旋转,其参数为旋转的速度与旋转的方向。
   transform.RotateAround():该方法用于设置模型围绕某一个点旋转。
   Time.deltaTime:用于记录上一帧所消耗的时间,这里用作模型旋转的速度系数。
   Vector3.right:x轴方向。
   Vector3.up:y轴方向。
   Vector3.forward:z轴方向。


作者:PEPE
出处:http://pepe.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/PEPE/p/3516528.html