我有几个游戏对象:每个都有一个“ Interactable”和“ Data”类。我还具有一个“ PauseGameController”类,该类未附加到那些我想动态使用每个对象的数据的游戏对象上,如果在按下Key I时触发了一个事件,并且“ Interactable”类的变量“ isInteracting”为true, 。换句话说,“ PauseGameController”类在一个特定对象发生这2个事件之前,不知道将使用哪个游戏对象数据。
我的问题是:
如何从未附加到该游戏对象的另一个脚本中动态检索在每个数据对象的“数据”类中的检查器上键入的数据?
我想重用“数据”和“可交互”类,并能够在场景中创建不同游戏对象时设置不同的数据。而且我不想使用“查找”功能,因为文档说它很慢。
代码: Interactables类
using UnityEngine;
using System.Collections;
public class Interactables : MonoBehaviour {
public static bool interactable = false;
public bool interactableown = false;
public Material[] material;
Renderer rend;
// Use this for initialization
void Start () {
rend = GetComponent<Renderer>();
rend.enabled = true;
rend.sharedMaterial = material[0];
}
// Update is called once per frame
void Update () {
if (interactableown)
{
rend.sharedMaterial = material[1];
}else
{
rend.sharedMaterial = material[0];
}
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Player")
{
interactable = true;
interactableown = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Player")
{
interactable = false;
interactableown = false;
}
}
}
暂停课程
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Puase : MonoBehaviour {
public static bool isPause = false;
public GameObject MenuUI;
public Text txt;
// Update is called once per frame
void Update () {
if (Input.GetKeyUp(KeyCode.I) && Interactables.interactable)
{
if (isPause)
{
Resume();
}
else
{
Pause();
}
}
}
public void Resume()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
MenuUI.SetActive(false);
Time.timeScale = 1f;
isPause = false;
}
public void Pause()
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
MenuUI.SetActive(true);
Time.timeScale = 0f;
isPause = true;
}
}
数据类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DataObj : MonoBehaviour {
public string info = "";
void Start () {
}
// Update is called once per frame
void Update () {
}
}
编辑者的身份 Capture