AkiraPixelWind/Assets/Axibug/Script/Runtime/Base/BaseComponent.cs
2022-12-29 18:20:40 +08:00

192 lines
5.6 KiB
C#

using Axibug.Resources;
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
namespace Axibug.Runtime
{
/// <summary>
/// 基础组件。
/// </summary>
[DisallowMultipleComponent]
[AddComponentMenu("Axibug/Base")]
public class BaseComponent : GameComponent
{
private const int DefaultDpi = 96; // default windows dpi
[SerializeField]
private bool m_EditorResourceMode = true;
[SerializeField]
private string m_TextHelperTypeName = "Axibug.Runtime.DefaultTextHelper";
[SerializeField]
private string m_LogHelperTypeName = "Axibug.Runtime.DefaultLogHelper";
[SerializeField]
private string m_VersionHelperTypeName = "Axibug.Runtime.DefaultVersionHelper";
[SerializeField]
private string m_JsonHelperTypeName = "Axibug.Runtime.DefaultJsonHelper";
//热更程序集
public Assembly m_Assembly;
/// <summary>
/// 获取或设置是否使用编辑器资源模式(仅编辑器内有效)。
/// </summary>
public bool EditorResourceMode
{
get
{
return m_EditorResourceMode;
}
set
{
m_EditorResourceMode = value;
}
}
/// <summary>
/// 游戏框架组件初始化。
/// </summary>
protected override void Awake()
{
base.Awake();
m_Assembly = null;
InitTextHelper();
InitVersionHelper();
InitLogHelper();
InitJsonHelper();
Utility.Converter.ScreenDpi = Screen.dpi;
if (Utility.Converter.ScreenDpi <= 0)
{
Utility.Converter.ScreenDpi = DefaultDpi;
}
}
private void Start()
{
//快速开发模式不用热更新和所有的资源管理
if (!EditorResourceMode)
{
AssetBundleManager.Init();
AssetManager.Init();
PoolsManager.Init();
PrefabManager.Init(gameObject);
}
}
private void InitTextHelper()
{
if (string.IsNullOrEmpty(m_TextHelperTypeName))
{
return;
}
Type textHelperType = Utility.Assembly.GetType(m_TextHelperTypeName);
if (textHelperType == null)
{
Log.Error("Can not find text helper type '{0}'.", m_TextHelperTypeName);
return;
}
Utility.Text.ITextHelper textHelper = (Utility.Text.ITextHelper)Activator.CreateInstance(textHelperType);
if (textHelper == null)
{
Log.Error("Can not create text helper instance '{0}'.", m_TextHelperTypeName);
return;
}
Utility.Text.SetTextHelper(textHelper);
}
private void InitLogHelper()
{
if (string.IsNullOrEmpty(m_LogHelperTypeName))
{
return;
}
Type logHelperType = Utility.Assembly.GetType(m_LogHelperTypeName);
if (logHelperType == null)
{
throw new GameException(Utility.Text.Format("Can not find log helper type '{0}'.", m_LogHelperTypeName));
}
AxibugLog.ILogHelper logHelper = (AxibugLog.ILogHelper)Activator.CreateInstance(logHelperType);
if (logHelper == null)
{
throw new GameException(Utility.Text.Format("Can not create log helper instance '{0}'.", m_LogHelperTypeName));
}
AxibugLog.SetLogHelper(logHelper);
}
private void InitVersionHelper()
{
if (string.IsNullOrEmpty(m_VersionHelperTypeName))
{
return;
}
Type versionHelperType = Utility.Assembly.GetType(m_VersionHelperTypeName);
if (versionHelperType == null)
{
throw new GameException(Utility.Text.Format("Can not find version helper type '{0}'.", m_VersionHelperTypeName));
}
Axibug.Version.IVersionHelper versionHelper = (Axibug.Version.IVersionHelper)Activator.CreateInstance(versionHelperType);
if (versionHelper == null)
{
throw new GameException(Utility.Text.Format("Can not create version helper instance '{0}'.", m_VersionHelperTypeName));
}
Axibug.Version.SetVersionHelper(versionHelper);
}
private void InitJsonHelper()
{
if (string.IsNullOrEmpty(m_JsonHelperTypeName))
{
return;
}
Type jsonHelperType = Utility.Assembly.GetType(m_JsonHelperTypeName);
if (jsonHelperType == null)
{
Log.Error("Can not find JSON helper type '{0}'.", m_JsonHelperTypeName);
return;
}
Utility.Json.IJsonHelper jsonHelper = (Utility.Json.IJsonHelper)Activator.CreateInstance(jsonHelperType);
if (jsonHelper == null)
{
Log.Error("Can not create JSON helper instance '{0}'.", m_JsonHelperTypeName);
return;
}
Utility.Json.SetJsonHelper(jsonHelper);
}
private void Update()
{
FrameworkEntry.Update(Time.deltaTime, Time.unscaledDeltaTime);
if (!EditorResourceMode)
PrefabManager.Update();
}
private void OnDestroy()
{
FrameworkEntry.Shutdown();
}
internal void Shutdown()
{
Destroy(gameObject);
}
}
}