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、运行效果:

  • 相关阅读:
    4-11 EurekaClient集成演示
    4-10 原始版服务调用演示
    4-9 Consumer内容准备
    4-8 Provider内容准备
    Swift:用UICollectionView整一个瀑布流
    Swift: 用Alamofire做http请求,用ObjectMapper解析JSON
    Swift: 用UserDefaults保存复杂对象
    BAT的真的适合创业团队吗?
    为什么要用GCD-Swift2.x
    Objective-C的泛型
  • 原文地址:https://www.cnblogs.com/vitah/p/3842313.html
Copyright © 2011-2022 走看看