游戏物体间的访问 (初学:2)

游戏对象成员组件的访问

  1. 游戏对象成员的访问,主要是指在脚本中对自身成员组件变量的访问,例如我们可以直接通过如下的代码引用物体中刚体组件
using System.Collections.Generic;
using UnityEngine;

public class ScriptB : MonoBehaviour
{
    // Start is called before the first frame update
    public GameObject Cube;//获取刚体组件
    private Rigidbody rig1;
    void Start()
    {
        rig1 = Cube.GetComponent<Rigidbody>();
        rig1.mass = Mathf.Sin(Time.time) + 1;
    }

    // Update is called once per frame
    void Update()
    {
		transform.Rotate(0,speed*Time.deltaTime,0);
    }
}

在本脚本里面涉及到了一个数学运算的函数Mathf.Sin(f:float),大致的意思是以弧度为单位计算并返回参数 f 中指定的数字的正弦值。

Time.time则是指从游戏开发到现在的时间,会随着游戏的暂停而停止计算
具体可以去这里看看
[https://blog.csdn.net/qq_33337811/article/details/54669494]
Time.deltaTime具体意义请看上一篇。
在Update()函数中可以像代码段里那样写,同样也可以写成

gameObject.transform.Rotate(0,speed*Time.deltaTime,0);

这是因为在编译器进行编译的时候,会默认此时的transform就是gameObject物体的成员变量,所以两种写法的作用是相同的。

自定义组件访问

脚本A的代码如下

using System.Collections.Generic;
using UnityEngine;

public class ScriptB : MonoBehaviour
{
    // Start is called before the first frame update
    NewBehaviourScript s = new NewBehaviourScript();  //var只能出现在局部变量的声明或者脚本代码里面
    public GameObject cube;//获取刚体组件
    private Rigidbody rig1;
    void Start()
    {
        rig1 = cube.GetComponent<Rigidbody>();//由于当前的这个脚本是挂在Cube上的所以默认为是Cube的rigidbody刚体组件
        rig1.mass = Mathf.Sin(Time.time) + 1;
        NewBehaviourScript.lianxi();
        s.wangchang();
    }

    // Update is called once per frame
    void Update()
    {
        
       transform.Rotate(0, Time.deltaTime * s.speed, 0);
    }
}

脚本B的代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    // Start is called before the first frame update
    public int speed = 5;
   
    void Start()
    {

        //GameObject.Find("Cube").SendMessage("Start");
       // Debug.Log("ScriptB.speedB");
    }

    // Update is called once per frame
    void Update()
    {
        //print("Hello World");
        transform.Rotate(0, speed * Time.deltaTime, 0);
    }
   
}

在脚本B中定义了一个公有整型变量 speed,如果想要在脚本A中使用这个变量共有两种方法,第一种:将变量设置为公有静态的,直接通过类名调用,如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    // Start is called before the first frame update
    public  statIC int speed = 5;
   
    void Start()
    {

        //GameObject.Find("Cube").SendMessage("Start");
       // Debug.Log("ScriptB.speedB");
    }

    // Update is called once per frame
    void Update()
    {
        //print("Hello World");
        transform.Rotate(0, speed * Time.deltaTime, 0);
    }
 }

然后在脚本A中直接通过对象名调用,如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptB : MonoBehaviour
{
	
    void Start()
    {
    	
    }

    // Update is called once per frame
    void Update()
    {
        
       transform.Rotate(0, Time.deltaTime *NewBehaviourScript .speed, 0);
    }
}

第二种,实例化被调用的变量所在的类,这里要注意一下实例化对象所在的类,假如是在Sta()函数方法中实例化的对象,那么在Update()的这个函数中是无法使用这个实例化的对象的,解决方法有两种,第一种将实例化的对象放在函数外面,类里面,如下

public class ScriptB : MonoBehaviour
{
    
    NewBehaviourScript s = new NewBehaviourScript();  //var只能出现在局部变量的声明或者脚本代码里面
   
    void Start()
    {
   
    }

    // Update is called once per frame
    void Update()
    {
 
    }
}

或者在Update()函数中实例化对象如下

void Update()
    {
        
       var = new NewBehaviourScript();
       Debug.Log("s.speed");
    }

需要注意的是,使用var实例化对象,只能出现在局部变量的声明或者脚本代码里面,也就是说不能在函数外面使用它进行实例化。
同样的,如果想要实现跨脚本的函数的调用,有三种方法。

第一种,被调用脚本函数为static类型,调用时直接用 脚本名.函数名()
被调用的脚本如下

public static void lianxi()
    {
        Debug.Log("流浪地球");
    }

调用函数的脚本如下

void Start()
    {
        var st = new NewBehaviourScript();
        st.wangchang();
    }

第二种,GameObject.Find(“脚本所在的物体的名字”).SendMessage(“函数名”); //能调用public和private类型函数。调用函数脚本如下

void Start()
    {
        GameObject.Find("NewBehaviourScript").SendMessage("lianxi");
    }

被调函数脚本如下

public void wangchang()
    {
        Debug.Log("wangchang ");
    }

第三种,GameObject.Find(“脚本所在的物体的名字”).GetComponent<脚本名>().函数名(); //只能调用public类型函数。
调用脚本函数如下

void Start()
    {
        GameObject.Find("Cube").GetComponent<NewBehaviourScript>().wangchang();
    }

如有错误,希望帮我更正,谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值