Skip to content

Commit 7a7b583

Browse files
committed
fixed an issue with automatic binding causing errors in Unity 6
1 parent 1c6a0dc commit 7a7b583

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

Assets/OxGFrame/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# CHANGELOG
22

3+
## [2.13.1] - 2025-01-09
4+
- Fixed an issue with BindCodeAutoGenerateEditor where using ScriptableObject.CreateInstance caused an "InvalidCastException: Specified cast is not valid." in Unity 6.
5+
36
## [2.13.0] - 2025-01-09
47
- Redefined ShowAnimation and HideAnimation methods in UIBase, marked as obsolete, and are no longer in use (must be replaced). Please replace them with OnShowAnimation and OnCloseAnimation for clearer method names.
58
- **Hint: You can use the menu item OxGFrame -> Others -> Try to update the APIs (Search and replace in all files) for automatic replacement.**

Assets/OxGFrame/CoreFrame/Scripts/Editor/BindCodeAutoGenerateEditor.cs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -531,8 +531,7 @@ public static string[] GetInheritedScriptPaths(MonoBehaviour script, bool includ
531531
if (!currentType.IsAbstract)
532532
{
533533
// 嘗試取得 MonoScript 路徑
534-
ScriptableObject tempInstance = ScriptableObject.CreateInstance(currentType);
535-
MonoScript monoScript = MonoScript.FromScriptableObject(tempInstance);
534+
MonoScript monoScript = _GetMonoScriptFromType(currentType);
536535

537536
if (monoScript != null)
538537
{
@@ -543,12 +542,6 @@ public static string[] GetInheritedScriptPaths(MonoBehaviour script, bool includ
543542
scriptPaths.Add(scriptPath);
544543
}
545544
}
546-
547-
// 清理暫時創建的實例
548-
if (tempInstance != null)
549-
{
550-
UnityEngine.Object.DestroyImmediate(tempInstance);
551-
}
552545
}
553546

554547
currentType = currentType.BaseType;
@@ -565,6 +558,30 @@ public static string[] GetInheritedScriptPaths(MonoBehaviour script, bool includ
565558
return scriptPaths.ToArray();
566559
}
567560

561+
/// <summary>
562+
/// 獲取 MonoScript
563+
/// </summary>
564+
/// <param name="type"></param>
565+
/// <returns></returns>
566+
private static MonoScript _GetMonoScriptFromType(Type type)
567+
{
568+
// 確保傳入的類型是 MonoBehaviour 的子類型
569+
if (!typeof(MonoBehaviour).IsAssignableFrom(type))
570+
return null;
571+
572+
// 嘗試通過反射訪問類型並獲取 MonoScript
573+
MonoBehaviour dummyInstance = new GameObject("Dummy").AddComponent(type) as MonoBehaviour;
574+
if (dummyInstance != null)
575+
{
576+
MonoScript monoScript = MonoScript.FromMonoBehaviour(dummyInstance);
577+
// 清理臨時對象
578+
UnityEngine.Object.DestroyImmediate(dummyInstance.gameObject);
579+
return monoScript;
580+
}
581+
582+
return null;
583+
}
584+
568585
/// <summary>
569586
/// 取得腳本中綁定區塊
570587
/// </summary>

Assets/OxGFrame/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "com.michaelo.oxgframe",
33
"displayName": "OxGFrame",
44
"description": "The OxGFrame is a framework based on Unity for accelerating game development. Supports multi-platform Win, OSX, Android, iOS, WebGL.",
5-
"version": "2.13.0",
5+
"version": "2.13.1",
66
"unity": "2021.3",
77
"license": "MIT",
88
"samples": [

0 commit comments

Comments
 (0)