Skip to content

Commit

Permalink
Basic method invoking
Browse files Browse the repository at this point in the history
  • Loading branch information
Voltstro committed Jan 29, 2024
1 parent f6c160b commit b4bb521
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/Packages/UnityWebBrowser/Runtime/Core/WebBrowserClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
// This project is under the MIT license. See the LICENSE.md file for more details.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -883,10 +886,44 @@ private void CheckIfIsReadyAndConnected()
#endregion

#region JS Methods

public struct JsMethodInfo
{
public MethodInfo Method { get; set; }
public object Target { get; set; }
}

private Dictionary<string, JsMethodInfo> jsMethod = new Dictionary<string, JsMethodInfo>();

public void RegisterJsMethod(string name, Action method)
{
//method.Method
jsMethod.Add(name, new JsMethodInfo
{
Method = method.GetMethodInfo(),
Target = method.Target
});
}

internal void InvokeJsMethod(ExecuteJsMethod executeJsMethod)
{
logger.Debug(executeJsMethod.MethodName);

KeyValuePair<string, JsMethodInfo> foundMethod = jsMethod.FirstOrDefault(x => x.Key == executeJsMethod.MethodName);
if (foundMethod.Key == null)
{
//Error
logger.Error($"Engine tried executing JS method '{executeJsMethod.MethodName}'! Which has not been registered!");
}

try
{
foundMethod.Value.Method.Invoke(foundMethod.Value.Target, null);
}
catch (Exception ex)
{
logger.Error($"Error executing JS method! {ex}");
}
}

#endregion
Expand Down
50 changes: 50 additions & 0 deletions src/UnityWebBrowser.UnityProject/Assets/Scenes/UWB.unity
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ PrefabInstance:
m_Modification:
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: 906994673, guid: eb44fadbfd943194e85df7faf3a9dc5b, type: 3}
propertyPath: browserClient.logSeverity
value: 0
objectReference: {fileID: 0}
- target: {fileID: 7454210984458747666, guid: eb44fadbfd943194e85df7faf3a9dc5b, type: 3}
propertyPath: m_Name
value: UnityWebBrowser
Expand Down Expand Up @@ -418,6 +422,51 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 53d0570114d34f649962da70092c0cea, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &1420301373
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1420301375}
- component: {fileID: 1420301374}
m_Layer: 0
m_Name: UWBPrjDemo
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1420301374
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1420301373}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 70dbcf8280994c52ae89774ef4b1a0c8, type: 3}
m_Name:
m_EditorClassIdentifier:
uiBasic: {fileID: 1162272219}
--- !u!4 &1420301375
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1420301373}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1460405560
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -591,3 +640,4 @@ MonoBehaviour:
- Width: 1920
Height: 1038
refreshRate: 1
hide: 0
26 changes: 26 additions & 0 deletions src/UnityWebBrowser.UnityProject/Assets/Scripts/UWBPrjDemo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// UnityWebBrowser (UWB)
// Copyright (c) 2021-2022 Voltstro-Studios
//
// This project is under the MIT license. See the LICENSE.md file for more details.

using System;
using UnityEngine;

namespace VoltstroStudios.UnityWebBrowser.Prj
{
public class UWBPrjDemo : MonoBehaviour
{
[SerializeField]
private WebBrowserUIBasic uiBasic;

public void Start()
{
uiBasic.browserClient.RegisterJsMethod("Test", TestMethod);
}

private void TestMethod()
{
Debug.Log("Hello from test method!");
}
}
}

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

0 comments on commit b4bb521

Please sign in to comment.