zoukankan      html  css  js  c++  java
  • Unity C# 游戏间物体间的访问

    脚本语言:C#

    1、在Unity工程中新建两个物体:Cube和Sphere

    2、分别为Cube和Sphere添加脚本CubeScript和SphereScript:

    在SphereScript这两个定义一个函数DoSomething(),脚本具体代码如下:

    using UnityEngine;
    using System.Collections;
    
    public class SphereScript : MonoBehaviour {
    
        public int radius = 5;
    
        // Use this for initialization
        void Start () {
        }
        
        // Update is called once per frame
        void Update () {
        }
    
        public void DoSomething(){
            Debug.Log("Hello ~");
        }
    }

    3、在CubeScript脚本中实现对物体Sphere的访问,使它的颜色变为红色,和脚本SphereScript中DoSomething函数的访问,具体代码如下:

    using UnityEngine;
    using System.Collections;
    
    public class CubeScript : MonoBehaviour {
    
        // Use this for initialization
        void Start () {
    
            SphereScript a = null;
            // 获取SphereScript脚本
            a = GameObject.Find("Sphere").GetComponent<SphereScript>();
            Debug.Log ("SphereScript's a = " + a.radius);
            Debug.Log("SphereScript's function Domething :");
            //调用SphereScript脚本的函数
            a.DoSomething ();
        }
        
        // Update is called once per frame
        void Update () {
            GameObject target = null;
            //访问Sphere引用物体
            target = GameObject.Find ("Sphere");
            //使球体颜色变为红色
            target.renderer.material.color = Color.red;
        }
    }

    4、运行效果:

  • 相关阅读:
    反射、枚举
    WebService、Http请求、Socket请求
    RPC和REST的区别
    命名分组
    golang isPowerOfTwo判断是否是2的幂
    golang 判断平台是32位还是64位
    vue的permission.js详解
    windows 下完全卸载oracle 11的详细过程
    freemarker导出word
    freemarker详细教程从入门到精通(三)模板入门与指令
  • 原文地址:https://www.cnblogs.com/vitah/p/3842313.html
Copyright © 2011-2022 走看看