91 lines
2.5 KiB
C#
91 lines
2.5 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace EnergyBarToolkit
|
|
{
|
|
public class EnergyBarSpawnerUGUI : MonoBehaviour
|
|
{
|
|
public enum FindCanvasMethod
|
|
{
|
|
ByTag = 0,
|
|
ByPath = 1,
|
|
ByType = 2
|
|
}
|
|
|
|
public EnergyBarUGUIBase barPrefab;
|
|
|
|
public Transform attachPoint;
|
|
|
|
public ObjectFinder canvas = new ObjectFinder(typeof(Canvas), "/Canvas", string.Empty, ObjectFinder.Method.ByPath);
|
|
|
|
public ObjectFinder worldCamera = new ObjectFinder(typeof(Camera), "/Main Camera", "MainCamera", ObjectFinder.Method.ByTag);
|
|
|
|
public bool updateLookupReference;
|
|
|
|
public bool networkInstantiate;
|
|
|
|
public int networkGroup;
|
|
|
|
[NonSerialized]
|
|
[HideInInspector]
|
|
public EnergyBarUGUIBase instance;
|
|
|
|
public static EnergyBarUGUIBase Instantiate(UnityEngine.Object parent, EnergyBarUGUIBase barPrefab, ObjectFinder canvasFinder, ObjectFinder cameraFinder, Transform attachPoint, bool networkInstantiate, int networkGroup, bool updateLookupReference)
|
|
{
|
|
EnergyBarUGUIBase energyBarUGUIBase = ((!networkInstantiate) ? UnityEngine.Object.Instantiate(barPrefab) : (Network.Instantiate(barPrefab, barPrefab.transform.position, barPrefab.transform.rotation, networkGroup) as EnergyBarUGUIBase));
|
|
if (canvasFinder.chosenMethod == ObjectFinder.Method.ByType)
|
|
{
|
|
energyBarUGUIBase.transform.SetParent(canvasFinder.Lookup<Canvas>(parent).transform, true);
|
|
}
|
|
else
|
|
{
|
|
energyBarUGUIBase.transform.SetParent(canvasFinder.Lookup<Transform>(parent), true);
|
|
}
|
|
EnergyBarFollowObject energyBarFollowObject = energyBarUGUIBase.GetComponent<EnergyBarFollowObject>();
|
|
if (energyBarFollowObject == null)
|
|
{
|
|
energyBarFollowObject = energyBarUGUIBase.gameObject.AddComponent<EnergyBarFollowObject>();
|
|
}
|
|
energyBarFollowObject.worldCamera = cameraFinder;
|
|
energyBarFollowObject.followObject = attachPoint.gameObject;
|
|
energyBarFollowObject.updateLookupReference = updateLookupReference;
|
|
return energyBarUGUIBase;
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
if (instance == null)
|
|
{
|
|
instance = Instantiate(this, barPrefab, canvas, worldCamera, attachPoint, networkInstantiate, networkGroup, updateLookupReference);
|
|
}
|
|
if (instance != null)
|
|
{
|
|
instance.gameObject.SetActive(true);
|
|
}
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (instance != null)
|
|
{
|
|
instance.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (instance != null)
|
|
{
|
|
if (networkInstantiate)
|
|
{
|
|
Network.Destroy(instance.gameObject);
|
|
}
|
|
else
|
|
{
|
|
UnityEngine.Object.Destroy(instance.gameObject);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|