focus-creative-games / hybridclr

HybridCLR是一个特性完整、零成本、高性能、低内存的Unity全平台原生c#热更新解决方案。 HybridCLR is a fully featured, zero-cost, high-performance, low-memory solution for Unity's all-platform native c# hotupdate.
https://code-philosophy.com/
MIT License
6.6k stars 670 forks source link

UNITY_EDITOR 模式使用Assembly.Load加载的Assembly,无法通过go.GetComponent<T>()获取热更资源上的脚本 #122

Closed guolijun110 closed 9 months ago

guolijun110 commented 9 months ago

Describe the bug | 描述问题 UNITY_EDITOR 模式使用Assembly.Load加载的Assembly,无法通过go.GetComponent()获取热更资源上的脚本

Enviroment | 环境

To Reproduce | 复制步骤

1.HotUpdate 下新增脚本SingletonMono.cs public abstract class SingletonMono : MonoBehaviour where T : SingletonMono { private static T _instance; public static T Instance { get { if (_instance != null) { return _instance; } Debug.Log("CB");

        //var prefab = AssetBundleLoaderMgr.instance.LoadResAsset<T>("Cube");
        //_instance = Instantiate(prefab);

        AssetBundle ab = AssetBundle.LoadFromMemory(LoadDll.ReadBytesFromStreamingAssets("prefabs"));
        GameObject cube = ab.LoadAsset<GameObject>("Cube");
        var data = GameObject.Instantiate(cube);
        data.name = $"{typeof(T).Name}";
        _instance = data.GetComponent<T>();
        return _instance;
    }
}

protected void Awake()
{
    _instance = this as T;
}
protected void OnDestroy()
{
    _instance = null;
}

} 2.修改 public class InstantiateByAsset : SingletonMono { public string text;

public void Log()
{
    Debug.Log($"[InstantiateByAsset] Instance:{text}, Success New");
}

} 3.StartGame 在 UNITY_EDITOR模式下也使用 _hotUpdateAss = Assembly.Load(ReadBytesFromStreamingAssets("HotUpdate.dll.bytes"));

4.Entry 的 Start调用 InstantiateByAsset.Instance.Log();,会直接报错Instance为空 原因是 image 无法获取到T .. 测试在window下发布打包是正常的,Editer模式下获取不到热更资源上的脚本

pirunxi commented 9 months ago

这个跟Hybridclr无关。你不应该在Eidtor下重复加载assembly。后续还有疑问直接新手群里问吧