85 lines
2.4 KiB
C#
85 lines
2.4 KiB
C#
using UnityEngine;
|
|
|
|
public class loadtopandbottomPrefab: MonoBehaviour
|
|
{
|
|
[Header("father obj")]
|
|
public GameObject top_and_bottom_Panel;
|
|
public GameObject gameobject_to_Instantiate_below;
|
|
[SerializeField] bool notify_Main_Panel = true;
|
|
|
|
void Start()
|
|
{
|
|
if (top_and_bottom_Panel != null && gameobject_to_Instantiate_below != null)
|
|
{
|
|
GameObject instantiated = FindExistingPanelInstance();
|
|
if (instantiated == null)
|
|
{
|
|
instantiated = Instantiate(top_and_bottom_Panel, gameobject_to_Instantiate_below.transform);
|
|
}
|
|
|
|
Canvas canvas = instantiated.GetComponent<Canvas>();
|
|
if (canvas != null)
|
|
{
|
|
Camera cam = Camera.main;
|
|
if (cam == null)
|
|
{
|
|
cam = SceneObjectLookupCache.FindAny<Camera>();
|
|
}
|
|
canvas.worldCamera = cam;
|
|
cam = null;
|
|
}
|
|
if (notify_Main_Panel)
|
|
{
|
|
Bind_Main_Panel(instantiated);
|
|
}
|
|
}
|
|
}
|
|
|
|
GameObject FindExistingPanelInstance()
|
|
{
|
|
if (gameobject_to_Instantiate_below == null || top_and_bottom_Panel == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
string prefabName = top_and_bottom_Panel.name;
|
|
Transform parent = gameobject_to_Instantiate_below.transform;
|
|
for (int i = 0; i < parent.childCount; i++)
|
|
{
|
|
Transform child = parent.GetChild(i);
|
|
if (child == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
bool nameMatches = child.name == prefabName || child.name == prefabName + "(Clone)";
|
|
bool hasBars = child.Find("TOP") != null || child.Find("BTM") != null;
|
|
if (nameMatches && hasBars)
|
|
{
|
|
return child.gameObject;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
void Bind_Main_Panel(GameObject instantiated)
|
|
{
|
|
if (instantiated == null)
|
|
{
|
|
return;
|
|
}
|
|
UI_Panel_Main main = SceneObjectLookupCache.FindAny<UI_Panel_Main>();
|
|
if (main == null)
|
|
{
|
|
return;
|
|
}
|
|
RectTransform top = instantiated.transform.Find("TOP") as RectTransform;
|
|
RectTransform bottom = instantiated.transform.Find("BTM") as RectTransform;
|
|
if (top != null || bottom != null)
|
|
{
|
|
main.Bind_Top_Bottom(top, bottom);
|
|
}
|
|
}
|
|
}
|