Forest_Client/Forest/Assets/PhxhSDK/Phxh/Utils/Utils.GameObject.cs

93 lines
2.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using UnityEngine;
namespace PhxhSDK
{
/// <summary>
/// gameobject对象工厂封装了gameobject的创建和销毁
/// </summary>
public static partial class Utils
{
private static Transform _defaultRoot;
public static GameObject CreateGameObject(bool dontDestroyOnLoad)
{
try
{
var go = new GameObject();
// if(dontDestroyOnLoad)GameObject.DontDestroyOnLoad(go);
go.transform.parent = GetDefaultRoot();
return go;
}
catch (Exception e)
{
DebugUtil.LogError(e);
}
return null;
}
public static void Destroy(GameObject go)
{
try
{
GameObject.Destroy(go);
}
catch (Exception e)
{
DebugUtil.LogError(e);
}
}
public static GameObject Clone(GameObject templateGo,GameObject parObj=null)
{
try
{
if (templateGo!=null)
{
Transform parent = null;
if (parObj != null && parObj.transform != null)
{
parent = parObj.transform;
}
if (parent == null)
{
parent = templateGo.transform.parent;
}
if (parent == null)
{
return GameObject.Instantiate(templateGo);
}
return GameObject.Instantiate(templateGo, parent, true);
}
}
catch (Exception e)
{
DebugUtil.LogError(e);
}
return null;
}
public static Transform GetDefaultRoot()
{
try
{
if (_defaultRoot == null)
{
GameObject gameObj = new GameObject("GameObjectFactoryRoot");
// Object.DontDestroyOnLoad(gameObj);
_defaultRoot = gameObj.transform;
}
}
catch (Exception e)
{
DebugUtil.LogError(e);
}
return _defaultRoot;
}
}
}