CommonParameterContainer改为Singleton模式
This commit is contained in:
@@ -5,7 +5,6 @@ using UnityEngine;
|
||||
|
||||
public class AgentController : MonoBehaviour
|
||||
{
|
||||
[SerializeField] public GameObject commonParameterContainerObj;
|
||||
[SerializeField] public Camera fpsCam;
|
||||
|
||||
[Header("GetAxis() Simulate")]
|
||||
@@ -41,7 +40,7 @@ public class AgentController : MonoBehaviour
|
||||
private void Start()
|
||||
{
|
||||
// initialize scripts
|
||||
commonPramCon = commonParameterContainerObj.GetComponent<CommonParameterContainer>();
|
||||
commonPramCon = CommonParameterContainer.Instance;
|
||||
playerController = transform.GetComponent<CharacterController>();
|
||||
|
||||
// initialize Environment parameters
|
||||
|
||||
@@ -2,7 +2,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class CommonParameterContainer : MonoBehaviour
|
||||
public class CommonParameterContainer : Singleton<CommonParameterContainer>
|
||||
{
|
||||
[SerializeField] private GameObject hudObj;
|
||||
|
||||
@@ -95,6 +95,7 @@ public class CommonParameterContainer : MonoBehaviour
|
||||
|
||||
private void Start()
|
||||
{
|
||||
Instance.persistThroughScene = false;
|
||||
messageCon = hudObj.GetComponent<MessageBoxController>();
|
||||
try
|
||||
{
|
||||
|
||||
@@ -8,7 +8,6 @@ using UnityEngine;
|
||||
public class MLAgentsCustomController : Agent
|
||||
{
|
||||
[SerializeField] private GameObject paramContainerObj;
|
||||
[SerializeField] private GameObject commonParameterContainer;
|
||||
[SerializeField] private GameObject targetControllerObj;
|
||||
[SerializeField] private GameObject environmentUIObj;
|
||||
[SerializeField] private GameObject sideChannelObj;
|
||||
@@ -50,7 +49,7 @@ public class MLAgentsCustomController : Agent
|
||||
agentController = transform.GetComponent<AgentController>();
|
||||
raySensors = transform.GetComponent<RaySensors>();
|
||||
paramContainer = paramContainerObj.GetComponent<ParameterContainer>();
|
||||
commonParamCon = commonParameterContainer.GetComponent<CommonParameterContainer>();
|
||||
commonParamCon = CommonParameterContainer.Instance;
|
||||
targetController = targetControllerObj.GetComponent<TargetController>();
|
||||
envUIController = environmentUIObj.GetComponent<EnvironmentUIControl>();
|
||||
hudController = hudObj.GetComponent<HUDController>();
|
||||
|
||||
@@ -7,7 +7,6 @@ public class ParameterContainer : MonoBehaviour
|
||||
[SerializeField] private GameObject blockConObj;
|
||||
[SerializeField] private GameObject agentObj;
|
||||
[SerializeField] private GameObject hudObj;
|
||||
[SerializeField] private GameObject CommonParamObj;
|
||||
|
||||
private TargetController targetCon;
|
||||
private SceneBlockContainer blockCont;
|
||||
@@ -56,7 +55,7 @@ public class ParameterContainer : MonoBehaviour
|
||||
targetCon = targetConObj.GetComponent<TargetController>();
|
||||
blockCont = blockConObj.GetComponent<SceneBlockContainer>();
|
||||
messageCon = hudObj.GetComponent<MessageBoxController>();
|
||||
commonParamCont = CommonParamObj.GetComponent<CommonParameterContainer>();
|
||||
commonParamCont = CommonParameterContainer.Instance;
|
||||
|
||||
areaTimeBonusPerSec = commonParamCont.areaTimeBonusPerSec;
|
||||
freeTimeBonusPerSec = commonParamCont.freeTimeBonusPerSec;
|
||||
|
||||
@@ -7,8 +7,6 @@ public class SceneBlockContainer : MonoBehaviour
|
||||
[SerializeField]
|
||||
private GameObject environmentObj;
|
||||
[SerializeField]
|
||||
private GameObject commonParameterContainerObj;
|
||||
[SerializeField]
|
||||
private GameObject hudObj;
|
||||
|
||||
// public GameObject[] attackBlockPrefabs = new GameObject[1];
|
||||
@@ -21,7 +19,7 @@ public class SceneBlockContainer : MonoBehaviour
|
||||
|
||||
private void Start()
|
||||
{
|
||||
commonParamCon = commonParameterContainerObj.GetComponent<CommonParameterContainer>();
|
||||
commonParamCon = CommonParameterContainer.Instance;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -10,7 +10,6 @@ public class TargetController : MonoBehaviour
|
||||
[SerializeField] private GameObject HUDObj;
|
||||
[SerializeField] private GameObject sceneBlockContainerObj;
|
||||
[SerializeField] private GameObject enemyContainerObj;
|
||||
[SerializeField] private GameObject commonParameterContainerObj;
|
||||
[SerializeField] private GameObject environmentUIObj;
|
||||
|
||||
// area
|
||||
@@ -70,7 +69,7 @@ public class TargetController : MonoBehaviour
|
||||
// Start is called before the first frame update
|
||||
private void Start()
|
||||
{
|
||||
commonParamCon = commonParameterContainerObj.GetComponent<CommonParameterContainer>();
|
||||
commonParamCon = CommonParameterContainer.Instance;
|
||||
sceneBlockCon = sceneBlockContainerObj.GetComponent<SceneBlockContainer>();
|
||||
envUICon = environmentUIObj.GetComponent<EnvironmentUIControl>();
|
||||
enemyCon = enemyContainerObj.GetComponent<EnemyContainer>();
|
||||
|
||||
@@ -21,8 +21,6 @@ public class MouseInMap : MonoBehaviour
|
||||
[SerializeField]
|
||||
private GameObject targetControllerObj;
|
||||
[SerializeField]
|
||||
private GameObject commonParameterContainerObj;
|
||||
[SerializeField]
|
||||
private GameObject HUDObj;
|
||||
|
||||
private Vector3 nowHitPosition = Vector3.zero;
|
||||
@@ -56,7 +54,7 @@ public class MouseInMap : MonoBehaviour
|
||||
|
||||
private void Start()
|
||||
{
|
||||
commonParamCon = commonParameterContainerObj.GetComponent<CommonParameterContainer>();
|
||||
commonParamCon = CommonParameterContainer.Instance;
|
||||
groundMask = LayerMask.GetMask("Ground");
|
||||
targetCon = targetControllerObj.GetComponent<TargetController>();
|
||||
mousePreviewCon = mousePreviewObj.GetComponent<MousePreview>();
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
|
||||
{
|
||||
private static T _instance;
|
||||
|
||||
public static T Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance == null)
|
||||
{
|
||||
_instance = (T)FindObjectOfType(typeof(T));
|
||||
if (_instance == null)
|
||||
{
|
||||
Debug.LogError("An instance of " + typeof(T) + " is needed in the scene, but there is none.");
|
||||
}
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
public bool persistThroughScene = false;
|
||||
|
||||
protected virtual void Awake()
|
||||
{
|
||||
if (_instance == null)
|
||||
{
|
||||
_instance = this as T;
|
||||
if (persistThroughScene)
|
||||
{
|
||||
DontDestroyOnLoad(gameObject);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a056057ed828e274b954ff7b8b2cb2f2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user