Files
bansonic_beta_main/Assets/scripts/UI/Panel/dontdestroyonload/btmandtopController.cs
T

163 lines
5.7 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
public class btmandtopController : MonoBehaviour
{
[Header("player unique so")]
public Player_SO player_SO;
[Header("put prefabs here")]
public GameObject putPrefabsHere;
[Header("son buttons")]
public Button settings_launch;
public Button userInfo_launch;
public Button store_launch;
public Button showLevel_display;
public Button email_display;
public Button notice_display;
[Header("sig prefabs")]
public GameObject showLevel_prefab;
public GameObject store_prefab;
public GameObject settings_prefab;
public GameObject userInfo_prefab;
public GameObject email_prefab;
public GameObject notice_prefab;
private UnityAction instantiateSettings;
private UnityAction instantiateUserInfo;
private UnityAction instantiateStore;
private UnityAction instantiateShowLevel;
private UnityAction instantiateEmail;
private UnityAction instantiateNotice;
// cached reference to the settings instance to ensure only one exists
private GameObject settingsInstance;
void Start()
{
instantiateSettings = () => ToggleSettingsPrefab();
instantiateUserInfo = () => InstantiatePrefab(userInfo_prefab);
instantiateStore = () => InstantiatePrefab(store_prefab);
instantiateShowLevel = () => InstantiatePrefab(showLevel_prefab);
instantiateEmail = () => InstantiatePrefab(email_prefab);
instantiateNotice = () => InstantiatePrefab(notice_prefab);
if (settings_launch != null)
settings_launch.onClick.AddListener(instantiateSettings);
if (userInfo_launch != null)
userInfo_launch.onClick.AddListener(instantiateUserInfo);
if (store_launch != null)
store_launch.onClick.AddListener(instantiateStore);
if (showLevel_display != null)
showLevel_display.onClick.AddListener(instantiateShowLevel);
if (email_display != null)
email_display.onClick.AddListener(instantiateEmail);
if (notice_display != null)
notice_display.onClick.AddListener(instantiateNotice);
// Pre-instantiate settings prefab and keep it inactive, ensuring only one exists
if (settings_prefab != null && putPrefabsHere != null)
{
// Try to find existing one in scene first
if (settingsInstance == null)
{
var existing = GameObject.Find(settings_prefab.name + "(Clone)");
if (existing != null)
{
settingsInstance = existing;
}
}
if (settingsInstance == null)
{
settingsInstance = Instantiate(settings_prefab, putPrefabsHere.transform);
settingsInstance.SetActive(false);
// try to set canvas camera now as in previous behavior
try
{
Canvas c = settingsInstance.GetComponentInChildren<Canvas>(true);
if (c != null)
{
Camera[] cams = GameObject.FindObjectsOfType<Camera>();
if (cams != null && cams.Length > 0)
{
c.worldCamera = cams[0];
}
cams = null;
c = null;
}
}
catch { }
}
else
{
// ensure it's parented correctly under putPrefabsHere
settingsInstance.transform.SetParent(putPrefabsHere.transform, false);
settingsInstance.SetActive(false);
}
}
}
void OnDestroy()
{
if (settings_launch != null)
settings_launch.onClick.RemoveListener(instantiateSettings);
if (userInfo_launch != null)
userInfo_launch.onClick.RemoveListener(instantiateUserInfo);
if (store_launch != null)
store_launch.onClick.RemoveListener(instantiateStore);
if (showLevel_display != null)
showLevel_display.onClick.RemoveListener(instantiateShowLevel);
if (email_display != null)
email_display.onClick.RemoveListener(instantiateEmail);
if (notice_display != null)
notice_display.onClick.RemoveListener(instantiateNotice);
}
private void ToggleSettingsPrefab()
{
if (settings_prefab == null || putPrefabsHere == null) return;
// If we don't have an instance (it may have been destroyed), instantiate one
if (settingsInstance == null)
{
settingsInstance = Instantiate(settings_prefab, putPrefabsHere.transform);
// attempt to set canvas camera
try
{
Canvas c = settingsInstance.GetComponentInChildren<Canvas>(true);
if (c != null)
{
Camera[] cams = GameObject.FindObjectsOfType<Camera>();
if (cams != null && cams.Length > 0)
{
c.worldCamera = cams[0];
}
cams = null;
c = null;
}
}
catch { }
settingsInstance.SetActive(true);
return;
}
// Toggle active state
bool active = settingsInstance.activeSelf;
settingsInstance.SetActive(!active);
}
private void InstantiatePrefab(GameObject prefab)
{
if (prefab != null && putPrefabsHere != null)
{
GameObject instance = Instantiate(prefab, putPrefabsHere.transform);
instance = null;
}
}
}