Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
完成 JSScriptableObject 基础功能
Browse files Browse the repository at this point in the history
  • Loading branch information
ialex32x committed May 11, 2021
1 parent 872da4d commit 787ef0f
Show file tree
Hide file tree
Showing 24 changed files with 1,124 additions and 1,085 deletions.
8 changes: 8 additions & 0 deletions Assets/Examples/Resources/data.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions Assets/Examples/Resources/data/js_data.asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c74456a7d35a921468a4a2a23d99cee4, type: 3}
m_Name: js_data
m_EditorClassIdentifier:
_scriptRef:
sourceFile: C:\Users\julio\Documents\Projects\github.com\ialex32x\unity-jsb\Scripts\src\example_scriptable_object.ts
modulePath: example_scriptable_object
className: MyScriptableObject
_properties:
_objects: []
_strings:
- key: value2
value: hello, world
_integers: []
_numbers:
- key: value1
value: 7
8 changes: 8 additions & 0 deletions Assets/Examples/Resources/data/js_data.asset.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Assets/jsb/Source/Binding/Values_push_class.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ public static JSValue js_push_classvalue(JSContext ctx, UnityEngine.Object o)
{
return JSApi.JS_UNDEFINED;
}

//TODO: 因为 ScriptableObject.OnEnable 的触发可能早于 Runtime 初始化, 需要一个地方补充一次脚本创建, 放在这里不合适
if (o is Unity.JSScriptableObject)
{
(o as Unity.JSScriptableObject).CreateScriptInstance();
}

return js_push_object(ctx, (object)o);
}
#endif
Expand Down
11 changes: 8 additions & 3 deletions Assets/jsb/Source/Unity/Editor/JSInspectorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace QuickJS.Unity
public abstract class JSInspectorBase<T> : Editor
where T : Object, IScriptEditorSupport
{
private T _target;
protected T _target;
protected JSScriptClassType _classType;

private string[] _tabViews = new string[] { "Editor", "Source", "Primitive" };
Expand Down Expand Up @@ -445,12 +445,17 @@ private void DrawScriptingView()
else
{
EditorGUILayout.HelpBox("Waiting for script instancing...", MessageType.Warning);
OnWaitingForScriptInstancing();
}
}

protected virtual void OnWaitingForScriptInstancing()
{
}

public override void OnInspectorGUI()
{
if (UnityEditor.EditorApplication.isCompiling)
if (EditorApplication.isCompiling || (!EditorApplication.isPlaying && EditorApplication.isPlayingOrWillChangePlaymode))
{
Release();
EditorGUILayout.HelpBox("Temporarily unavailable in the script compilation process", MessageType.Warning);
Expand All @@ -472,4 +477,4 @@ public override void OnInspectorGUI()
}
}
}
#endif
#endif
8 changes: 8 additions & 0 deletions Assets/jsb/Source/Unity/Editor/JSScriptableObjectInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ protected override JSScriptClassType GetScriptClassType()
{
return JSScriptClassType.ScriptableObject;
}

protected override void OnWaitingForScriptInstancing()
{
if (_target.enabled)
{
_target.CreateScriptInstance();
}
}
}
}
#endif
2 changes: 2 additions & 0 deletions Assets/jsb/Source/Unity/JSBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class JSBehaviour : MonoBehaviour, ISerializationCallbackReceiver, IScrip
// self controlled script instance lifetime
private bool _isStandaloneScript = false;
public bool isStandaloneScript => _isStandaloneScript;
#else
public bool isStandaloneScript => true;
#endif

private JSContext _ctx = JSContext.Null;
Expand Down
63 changes: 20 additions & 43 deletions Assets/jsb/Source/Unity/JSScriptableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ namespace QuickJS.Unity
using UnityEngine;
using UnityEngine.Serialization;

//TODO: 因为 ScriptableObject.OnEnable 的触发可能早于 Runtime 初始化, 需要一个地方补充一次脚本创建
//TODO: 相关临时代码目前位于 Values_push_class.cs: public static JSValue js_push_classvalue(JSContext ctx, UnityEngine.Object o)

[CreateAssetMenu(fileName = "js_data", menuName = "JSScriptableObject Asset", order = 100)]
public class JSScriptableObject : ScriptableObject, ISerializationCallbackReceiver, IScriptEditorSupport
{
Expand All @@ -22,15 +25,16 @@ public class JSScriptableObject : ScriptableObject, ISerializationCallbackReceiv
// internal use only
public JSScriptProperties properties => _properties;

private bool _enabled;

public bool enabled => _enabled;

private bool _isScriptInstanced = false;

public bool isScriptInstanced => _isScriptInstanced;

#if UNITY_EDITOR
// self controlled script instance lifetime
private bool _isStandaloneScript = false;
public bool isStandaloneScript => _isStandaloneScript;
#endif
public bool isStandaloneScript => true;

JSScriptRef IScriptEditorSupport.scriptRef { get { return _scriptRef; } set { _scriptRef = value; } }

Expand All @@ -39,9 +43,6 @@ public class JSScriptableObject : ScriptableObject, ISerializationCallbackReceiv
private JSContext _ctx = JSContext.Null;
private JSValue _this_obj = JSApi.JS_UNDEFINED;

private bool _onDestroyValid;
private JSValue _onDestroyFunc = JSApi.JS_UNDEFINED;

private bool _onBeforeSerializeValid;
private JSValue _onBeforeSerializeFunc = JSApi.JS_UNDEFINED;

Expand All @@ -60,12 +61,10 @@ public bool IsValid()

public void ReleaseJSValues()
{
var context = ScriptEngine.GetContext(_ctx);

if (!_this_obj.IsNullish())
{
JSApi.JS_FreeValue(_ctx, _onDestroyFunc);
_onDestroyFunc = JSApi.JS_UNDEFINED;
_onDestroyValid = false;

JSApi.JS_FreeValue(_ctx, _onBeforeSerializeFunc);
_onBeforeSerializeFunc = JSApi.JS_UNDEFINED;
_onBeforeSerializeValid = false;
Expand All @@ -77,8 +76,8 @@ public void ReleaseJSValues()
JSApi.JS_FreeValue(_ctx, _this_obj);
_this_obj = JSApi.JS_UNDEFINED;
}

_isScriptInstanced = false;
var context = ScriptEngine.GetContext(_ctx);
_ctx = JSContext.Null;

if (context != null)
Expand Down Expand Up @@ -153,7 +152,7 @@ public bool CreateScriptInstance()
}
else
{
Debug.LogError("script runtime not ready");
// Debug.LogError("script runtime not ready");
}
}
else
Expand Down Expand Up @@ -249,9 +248,6 @@ private void _SetScriptInstance(JSContext ctx, JSValue this_obj, bool execAwake)
_onAfterDeserializeFunc = JSApi.JS_GetProperty(ctx, this_obj, context.GetAtom("OnAfterDeserialize"));
_onAfterDeserializeValid = JSApi.JS_IsFunction(ctx, _onAfterDeserializeFunc) == 1;

_onDestroyFunc = JSApi.JS_GetProperty(ctx, this_obj, context.GetAtom("OnDestroy"));
_onDestroyValid = JSApi.JS_IsFunction(ctx, _onDestroyFunc) == 1;

this._OnScriptingAfterDeserialize();
}
}
Expand Down Expand Up @@ -280,29 +276,6 @@ public void ReleaseScriptInstance()
ReleaseJSValues();
}

void OnDisable()
{
#if UNITY_EDITOR
if (UnityEditor.EditorApplication.isCompiling)
{
ReleaseJSValues();
}
#endif
}

void OnDestroy()
{
if (_onDestroyValid)
{
var rval = JSApi.JS_Call(_ctx, _onDestroyFunc, _this_obj);
if (rval.IsException())
{
_ctx.print_exception();
}
JSApi.JS_FreeValue(_ctx, rval);
}
ReleaseJSValues();
}
public void OnBeforeSerialize()
{
if (_onBeforeSerializeValid)
Expand Down Expand Up @@ -337,14 +310,18 @@ public void OnAfterDeserialize()
{
}

void Awake()
void OnEnable()
{
#if UNITY_EDITOR
_isStandaloneScript = true;
#endif
_enabled = true;
CreateScriptInstance();
}

void OnDisable()
{
_enabled = false;
ReleaseJSValues();
}

public void _OnScriptingAfterDeserialize()
{
if (_onAfterDeserializeValid)
Expand Down
14 changes: 7 additions & 7 deletions Scripts/out/data.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 13 additions & 13 deletions Scripts/out/editor/js_asset_process.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Scripts/out/editor/js_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Scripts/out/editor/js_test2.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 787ef0f

Please sign in to comment.