2
0
Эх сурвалжийг харах

Resource handle refactor complete

BearishSun 10 жил өмнө
parent
commit
b5bfb88590
56 өөрчлөгдсөн 502 нэмэгдсэн , 197 устгасан
  1. 1 1
      BansheeCore/Include/BsResources.h
  2. 2 2
      BansheeEditor/Include/BsPlatformInfo.h
  3. 4 4
      BansheeEditor/Include/BsPlatformInfoRTTI.h
  4. 1 1
      BansheeEngine/Include/BsGameSettings.h
  5. 3 3
      BansheeEngine/Include/BsGameSettingsRTTI.h
  6. 3 3
      BansheeMono/Include/BsMonoUtil.h
  7. 2 2
      BansheeMono/Source/BsMonoArray.cpp
  8. 1 1
      Game/Source/Main.cpp
  9. 4 4
      MBansheeEditor/BuildManager.cs
  10. 4 4
      MBansheeEditor/BuildWindow.cs
  11. 25 3
      MBansheeEditor/GUI/GUIResourceField.cs
  12. 25 3
      MBansheeEditor/GUI/GUITextureField.cs
  13. 4 2
      MBansheeEditor/Inspector/InspectableResourceRef.cs
  14. 10 2
      MBansheeEditor/Inspectors/GUISkinInspector.cs
  15. 12 4
      MBansheeEditor/Inspectors/MaterialInspector.cs
  16. 6 2
      MBansheeEditor/Inspectors/RenderableInspector.cs
  17. 2 1
      MBansheeEditor/Inspectors/SpriteTextureInspector.cs
  18. 34 9
      MBansheeEngine/ResourceRef.cs
  19. 17 1
      MBansheeEngine/Resources.cs
  20. 12 2
      SBansheeEditor/Include/BsGUIResourceField.h
  21. 14 3
      SBansheeEditor/Include/BsGUITextureField.h
  22. 4 2
      SBansheeEditor/Include/BsScriptGUIResourceField.h
  23. 4 2
      SBansheeEditor/Include/BsScriptGUITextureField.h
  24. 2 2
      SBansheeEditor/Include/BsScriptPlatformInfo.h
  25. 34 1
      SBansheeEditor/Source/BsGUIResourceField.cpp
  26. 15 1
      SBansheeEditor/Source/BsGUITextureField.cpp
  27. 3 3
      SBansheeEditor/Source/BsScriptBrowseDialog.cpp
  28. 8 8
      SBansheeEditor/Source/BsScriptBuildManager.cpp
  29. 2 2
      SBansheeEditor/Source/BsScriptDragDropManager.cpp
  30. 12 12
      SBansheeEditor/Source/BsScriptEditorApplication.cpp
  31. 2 2
      SBansheeEditor/Source/BsScriptEditorBuiltin.cpp
  32. 3 3
      SBansheeEditor/Source/BsScriptEditorSettings.cpp
  33. 5 5
      SBansheeEditor/Source/BsScriptFolderMonitor.cpp
  34. 26 4
      SBansheeEditor/Source/BsScriptGUIResourceField.cpp
  35. 3 3
      SBansheeEditor/Source/BsScriptGUITextField.cpp
  36. 27 4
      SBansheeEditor/Source/BsScriptGUITextureField.cpp
  37. 15 24
      SBansheeEditor/Source/BsScriptPlatformInfo.cpp
  38. 9 9
      SBansheeEditor/Source/BsScriptProjectLibrary.cpp
  39. 2 2
      SBansheeEditor/Source/BsScriptProjectSettings.cpp
  40. 3 3
      SBansheeEditor/Source/BsScriptSelection.cpp
  41. 2 0
      SBansheeEngine/Include/BsScriptEnginePrerequisites.h
  42. 57 10
      SBansheeEngine/Include/BsScriptResourceRef.h
  43. 1 0
      SBansheeEngine/Include/BsScriptResources.h
  44. 1 1
      SBansheeEngine/Source/BsManagedSerializableField.cpp
  45. 2 2
      SBansheeEngine/Source/BsScriptDebug.cpp
  46. 2 2
      SBansheeEngine/Source/BsScriptGUIElement.cpp
  47. 2 2
      SBansheeEngine/Source/BsScriptGUIInputBox.cpp
  48. 1 1
      SBansheeEngine/Source/BsScriptHString.cpp
  49. 1 1
      SBansheeEngine/Source/BsScriptPlainText.cpp
  50. 2 2
      SBansheeEngine/Source/BsScriptResource.cpp
  51. 50 21
      SBansheeEngine/Source/BsScriptResourceRef.cpp
  52. 10 0
      SBansheeEngine/Source/BsScriptResources.cpp
  53. 1 1
      SBansheeEngine/Source/BsScriptSceneObject.cpp
  54. 2 2
      SBansheeEngine/Source/BsScriptScriptCode.cpp
  55. 1 1
      SBansheeEngine/Source/BsScriptSerializableField.cpp
  56. 2 2
      SBansheeEngine/Source/BsScriptStringTable.cpp

+ 1 - 1
BansheeCore/Include/BsResources.h

@@ -58,7 +58,7 @@ namespace BansheeEngine
 		template <class T>
 		ResourceHandle<T> load(const WeakResourceHandle<T>& handle, bool loadDependencies = true)
 		{
-			return static_resource_cast<T>(load(handle, loadDependencies));
+			return static_resource_cast<T>(load((const WeakResourceHandle<Resource>&)handle, loadDependencies));
 		}
 
 		/**

+ 2 - 2
BansheeEditor/Include/BsPlatformInfo.h

@@ -25,7 +25,7 @@ namespace BansheeEngine
 
 		PlatformType type; /**< Type of platform this object contains data for. */
 		WString defines; /**< A set of semicolon separated defines to use when compiling scripts for this platform. */
-		HPrefab mainScene; /**< Default scene that is loaded when the application is started. */
+		WeakResourceHandle<Prefab> mainScene; /**< Default scene that is loaded when the application is started. */
 		bool fullscreen; /**< If true the application will be started in fullscreen using user's desktop resolution. */
 		UINT32 windowedWidth; /**< Width of the window if not starting the application in fullscreen. */
 		UINT32 windowedHeight; /**< Height of the window if not starting the application in fullscreen. */
@@ -48,7 +48,7 @@ namespace BansheeEngine
 	{
 		WinPlatformInfo();
 
-		HTexture icon;
+		WeakResourceHandle<Texture> icon;
 		WString titlebarText;
 
 		/************************************************************************/

+ 4 - 4
BansheeEditor/Include/BsPlatformInfoRTTI.h

@@ -15,8 +15,8 @@ namespace BansheeEngine
 		PlatformType& getType(PlatformInfo* obj) { return obj->type; }
 		void setType(PlatformInfo* obj, PlatformType& val) { obj->type = val; }
 
-		HPrefab& getMainScene(PlatformInfo* obj) { return obj->mainScene; }
-		void setMainScene(PlatformInfo* obj, HPrefab& val) { obj->mainScene = val; }
+		WeakResourceHandle<Prefab>& getMainScene(PlatformInfo* obj) { return obj->mainScene; }
+		void setMainScene(PlatformInfo* obj, WeakResourceHandle<Prefab>& val) { obj->mainScene = val; }
 
 		bool& getFullscreen(PlatformInfo* obj) { return obj->fullscreen; }
 		void setFullscreen(PlatformInfo* obj, bool& val) { obj->fullscreen = val; }
@@ -62,8 +62,8 @@ namespace BansheeEngine
 	class BS_ED_EXPORT WinPlatformInfoRTTI : public RTTIType <WinPlatformInfo, PlatformInfo, WinPlatformInfoRTTI>
 	{
 	private:
-		HTexture& getIcon(WinPlatformInfo* obj) { return obj->icon; }
-		void setIcon(WinPlatformInfo* obj, HTexture& val) { obj->icon = val; }
+		WeakResourceHandle<Texture>& getIcon(WinPlatformInfo* obj) { return obj->icon; }
+		void setIcon(WinPlatformInfo* obj, WeakResourceHandle<Texture>& val) { obj->icon = val; }
 
 		WString& getTitlebarText(WinPlatformInfo* obj) { return obj->titlebarText; }
 		void setTitlebarText(WinPlatformInfo* obj, WString& val) { obj->titlebarText = val; }

+ 1 - 1
BansheeEngine/Include/BsGameSettings.h

@@ -12,7 +12,7 @@ namespace BansheeEngine
 	class BS_EXPORT GameSettings : public IReflectable
 	{
 	public:
-		String mainSceneUUID; /**< Resource UUID of the default scene that is loaded when the application is started. */
+		WeakResourceHandle<Prefab> mainScene; /**< Resource UUID of the default scene that is loaded when the application is started. */
 		bool fullscreen = true; /**< If true the application will be started in fullscreen using user's desktop resolution. */
 		bool useDesktopResolution = true; /**< If running in fullscreen should the user's desktop resolution be used instead of the specified resolution. */
 		UINT32 resolutionWidth = 1280; /**< Width of the window. */

+ 3 - 3
BansheeEngine/Include/BsGameSettingsRTTI.h

@@ -9,8 +9,8 @@ namespace BansheeEngine
 	class BS_EXPORT GameSettingsRTTI : public RTTIType <GameSettings, IReflectable, GameSettingsRTTI>
 	{
 	private:
-		String& getMainSceneUUID(GameSettings* obj) { return obj->mainSceneUUID; }
-		void setMainSceneUUID(GameSettings* obj, String& val) { obj->mainSceneUUID = val; }
+		WeakResourceHandle<Prefab>& getMainScene(GameSettings* obj) { return obj->mainScene; }
+		void setMainScene(GameSettings* obj, WeakResourceHandle<Prefab>& val) { obj->mainScene = val; }
 
 		bool& getFullscreen(GameSettings* obj) { return obj->fullscreen; }
 		void setFullscreen(GameSettings* obj, bool& val) { obj->fullscreen = val; }
@@ -30,7 +30,7 @@ namespace BansheeEngine
 	public:
 		GameSettingsRTTI()
 		{
-			addPlainField("mainSceneUUID", 0, &GameSettingsRTTI::getMainSceneUUID, &GameSettingsRTTI::setMainSceneUUID);
+			addReflectableField("mainScene", 0, &GameSettingsRTTI::getMainScene, &GameSettingsRTTI::setMainScene);
 			addPlainField("fullscreen", 1, &GameSettingsRTTI::getFullscreen, &GameSettingsRTTI::setFullscreen);
 			addPlainField("useDesktopResolution", 2, &GameSettingsRTTI::getUseDesktopResolution, &GameSettingsRTTI::setUseDesktopResolution);
 			addPlainField("resolutionWidth", 3, &GameSettingsRTTI::getResolutionWidth, &GameSettingsRTTI::setResolutionWidth);

+ 3 - 3
BansheeMono/Include/BsMonoUtil.h

@@ -55,7 +55,7 @@ namespace BansheeEngine
 		/**
 		 * @brief	Converts a native wide string to a Mono (i.e. managed) string.
 		 */
-		static MonoString* wstringToMono(MonoDomain* domain, const WString& str)
+		static MonoString* wstringToMono(const WString& str)
 		{
 			if (sizeof(wchar_t) == 2) // Assuming UTF-16
 				return mono_string_from_utf16((mono_unichar2*)str.c_str());
@@ -76,9 +76,9 @@ namespace BansheeEngine
 		/**
 		 * @brief	Converts a native narrow string to a Mono (i.e. managed) string.
 		 */
-		static MonoString* stringToMono(MonoDomain* domain, const String& str)
+		static MonoString* stringToMono(const String& str)
 		{
-			return wstringToMono(domain, toWString(str));
+			return wstringToMono(toWString(str));
 		}
 
 		/**

+ 2 - 2
BansheeMono/Source/BsMonoArray.cpp

@@ -38,14 +38,14 @@ namespace BansheeEngine
 	template<>
 	void ScriptArray::set<String>(UINT32 idx, const String& value)
 	{
-		MonoString* monoString = MonoUtil::stringToMono(MonoManager::instance().getDomain(), value);
+		MonoString* monoString = MonoUtil::stringToMono(value);
 		mono_array_set(mInternal, MonoString*, idx, monoString);
 	}
 
 	template<>
 	void ScriptArray::set<WString>(UINT32 idx, const WString& value)
 	{
-		MonoString* monoString = MonoUtil::wstringToMono(MonoManager::instance().getDomain(), value);
+		MonoString* monoString = MonoUtil::wstringToMono(value);
 		mono_array_set(mInternal, MonoString*, idx, monoString);
 	}
 

+ 1 - 1
Game/Source/Main.cpp

@@ -121,7 +121,7 @@ void runApplication()
 		gResources().registerResourceManifest(manifest);
 	}
 
-	HPrefab mainScene = static_resource_cast<Prefab>(gResources().loadFromUUID(gameSettings->mainSceneUUID));
+	HPrefab mainScene = gResources().load<Prefab>(gameSettings->mainScene);
 	if (mainScene != nullptr)
 	{
 		HSceneObject root = mainScene->instantiate();

+ 4 - 4
MBansheeEditor/BuildManager.cs

@@ -37,7 +37,7 @@ namespace BansheeEditor
         /// <summary>
         /// Initial scene that is loaded when application is first started.
         /// </summary>
-        public Prefab MainScene
+        public ResourceRef<Prefab> MainScene
         {
             get { return Internal_GetMainScene(mCachedPtr); }
             set
@@ -121,7 +121,7 @@ namespace BansheeEditor
         private static extern void Internal_SetDefines(IntPtr thisPtr, string value);
 
         [MethodImpl(MethodImplOptions.InternalCall)]
-        private static extern Prefab Internal_GetMainScene(IntPtr thisPtr);
+        private static extern ResourceRef<Prefab> Internal_GetMainScene(IntPtr thisPtr);
 
         [MethodImpl(MethodImplOptions.InternalCall)]
         static extern void Internal_SetMainScene(IntPtr thisPtr, IntPtr prefabPtr);
@@ -168,7 +168,7 @@ namespace BansheeEditor
         /// <summary>
         /// Texture that will be displayed on the application's executable.
         /// </summary>
-        public Texture2D Icon
+        public ResourceRef<Texture2D> Icon
         {
             get { return Internal_GetIcon(mCachedPtr); }
             set
@@ -191,7 +191,7 @@ namespace BansheeEditor
         }
         
         [MethodImpl(MethodImplOptions.InternalCall)]
-        private static extern Texture2D Internal_GetIcon(IntPtr thisPtr);
+        private static extern ResourceRef<Texture2D> Internal_GetIcon(IntPtr thisPtr);
 
         [MethodImpl(MethodImplOptions.InternalCall)]
         private static extern void Internal_SetIcon(IntPtr thisPtr, IntPtr texturePtr);

+ 4 - 4
MBansheeEditor/BuildWindow.cs

@@ -179,7 +179,7 @@ namespace BansheeEditor
             layout.AddElement(definesField);
             layout.AddSpace(5);
 
-            sceneField.Value = platformInfo.MainScene;
+            sceneField.ValueRef = platformInfo.MainScene;
             debugToggle.Value = platformInfo.Debug;
             definesField.Value = platformInfo.Defines;
             fullscreenField.Value = platformInfo.Fullscreen;
@@ -192,7 +192,7 @@ namespace BansheeEditor
                 heightField.Active = false;
             }
 
-            sceneField.OnChanged += x => platformInfo.MainScene = (Prefab)x;
+            sceneField.OnChanged += x => platformInfo.MainScene = x as ResourceRef<Prefab>;
             debugToggle.OnChanged += x => platformInfo.Debug = x;
             definesField.OnChanged += x => platformInfo.Defines = x;
             fullscreenField.OnChanged += x =>
@@ -220,10 +220,10 @@ namespace BansheeEditor
                     layout.AddElement(iconField);
 
                     titleField.Value = winPlatformInfo.TitleText;
-                    iconField.Value = winPlatformInfo.Icon;
+                    iconField.ValueRef = winPlatformInfo.Icon;
 
                     titleField.OnChanged += x => winPlatformInfo.TitleText = x;
-                    iconField.OnChanged += x => winPlatformInfo.Icon = x as Texture2D;
+                    iconField.OnChanged += x => winPlatformInfo.Icon = x as ResourceRef<Texture2D>;
                 }
                     break;
             }

+ 25 - 3
MBansheeEditor/GUI/GUIResourceField.cs

@@ -10,7 +10,7 @@ namespace BansheeEditor
     /// </summary>
     public sealed class GUIResourceField : GUIElement
     {
-        public delegate void OnChangedDelegate(Resource newValue);
+        public delegate void OnChangedDelegate(ResourceRefBase newValue);
 
         /// <summary>
         /// Triggered when the value in the field changes.
@@ -18,7 +18,8 @@ namespace BansheeEditor
         public event OnChangedDelegate OnChanged;
 
         /// <summary>
-        /// <see cref="Resource"/> referenced by the field.
+        /// <see cref="Resource"/> referenced by the field. This will load the resource if it is not already loaded. Use
+        /// <see cref="ValueRef"/> if you don't require a loaded resource.
         /// </summary>
         public Resource Value
         {
@@ -32,6 +33,21 @@ namespace BansheeEditor
             set { Internal_SetValue(mCachedPtr, value); }
         }
 
+        /// <summary>
+        /// Reference to the <see cref="Resource"/> referenced by the field.
+        /// </summary>
+        public ResourceRefBase ValueRef
+        {
+            get
+            {
+                ResourceRefBase value;
+                Internal_GetValueRef(mCachedPtr, out value);
+                return value;
+            }
+
+            set { Internal_SetValueRef(mCachedPtr, value); }
+        }
+
         /// <summary>
         /// Creates a new resource field element with a label.
         /// </summary>
@@ -75,7 +91,7 @@ namespace BansheeEditor
         /// Triggered by the runtime when the value of the field changes.
         /// </summary>
         /// <param name="newValue">New resource referenced by the field.</param>
-        private void DoOnChanged(Resource newValue)
+        private void DoOnChanged(ResourceRefBase newValue)
         {
             if (OnChanged != null)
                 OnChanged(newValue);
@@ -91,6 +107,12 @@ namespace BansheeEditor
         [MethodImpl(MethodImplOptions.InternalCall)]
         private static extern void Internal_SetValue(IntPtr nativeInstance, Resource value);
 
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern void Internal_GetValueRef(IntPtr nativeInstance, out ResourceRefBase value);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern void Internal_SetValueRef(IntPtr nativeInstance, ResourceRefBase value);
+
         [MethodImpl(MethodImplOptions.InternalCall)]
         private static extern void Internal_SetTint(IntPtr nativeInstance, Color color);
     }

+ 25 - 3
MBansheeEditor/GUI/GUITextureField.cs

@@ -11,7 +11,7 @@ namespace BansheeEditor
     /// </summary>
     public sealed class GUITextureField : GUIElement
     {
-        public delegate void OnChangedDelegate(Texture newValue);
+        public delegate void OnChangedDelegate(ResourceRefBase newValue);
 
         /// <summary>
         /// Triggered when the value in the field changes.
@@ -19,7 +19,8 @@ namespace BansheeEditor
         public event OnChangedDelegate OnChanged;
 
         /// <summary>
-        /// <see cref="Texture"/> referenced by the field.
+        /// <see cref="Texture"/> referenced by the field. This will load the texture if it is not already loaded. Use
+        /// <see cref="ValueRef"/> if you don't require a loaded resource.
         /// </summary>
         public Texture Value
         {
@@ -33,6 +34,21 @@ namespace BansheeEditor
             set { Internal_SetValue(mCachedPtr, value); }
         }
 
+        /// <summary>
+        /// Reference to the <see cref="Texture"/> referenced by the field.
+        /// </summary>
+        public ResourceRefBase ValueRef
+        {
+            get
+            {
+                ResourceRefBase value;
+                Internal_GetValueRef(mCachedPtr, out value);
+                return value;
+            }
+
+            set { Internal_SetValueRef(mCachedPtr, value); }
+        }
+
         /// <summary>
         /// Creates a new texture field element with a label.
         /// </summary>
@@ -74,7 +90,7 @@ namespace BansheeEditor
         /// Triggered by the runtime when the value of the field changes.
         /// </summary>
         /// <param name="newValue">New resource referenced by the field.</param>
-        private void Internal_DoOnChanged(Texture newValue)
+        private void Internal_DoOnChanged(ResourceRefBase newValue)
         {
             if (OnChanged != null)
                 OnChanged(newValue);
@@ -90,6 +106,12 @@ namespace BansheeEditor
         [MethodImpl(MethodImplOptions.InternalCall)]
         private static extern void Internal_SetValue(IntPtr nativeInstance, Texture value);
 
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern void Internal_GetValueRef(IntPtr nativeInstance, out ResourceRefBase value);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern void Internal_SetValueRef(IntPtr nativeInstance, ResourceRefBase value);
+
         [MethodImpl(MethodImplOptions.InternalCall)]
         private static extern void Internal_SetTint(IntPtr nativeInstance, Color color);
     }

+ 4 - 2
MBansheeEditor/Inspector/InspectableResourceRef.cs

@@ -53,9 +53,11 @@ namespace BansheeEditor
         /// Triggered when the user drops a new resource onto the field, or clears the current value.
         /// </summary>
         /// <param name="newValue">New resource to reference.</param>
-        private void OnFieldValueChanged(Resource newValue)
+        private void OnFieldValueChanged(ResourceRefBase newValue)
         {
-            property.SetValue(newValue);
+            Resource res = Resources.Load<Resource>(newValue);
+
+            property.SetValue(res);
             state = InspectableState.Modified;
         }
     }

+ 10 - 2
MBansheeEditor/Inspectors/GUISkinInspector.cs

@@ -279,7 +279,14 @@ namespace BansheeEditor
                     isExpanded = x;
                 };
 
-                fontField.OnChanged += x => { GetStyle().Font = (Font) x; MarkAsModified(); ConfirmModify(); };
+                fontField.OnChanged += x => 
+                {
+                    Font font = Resources.Load<Font>(x);
+
+                    GetStyle().Font = font; 
+                    MarkAsModified(); 
+                    ConfirmModify(); 
+                };
                 fontSizeField.OnChanged += x => { GetStyle().FontSize = x; MarkAsModified(); };
                 fontSizeField.OnFocusLost += ConfirmModify;
                 fontSizeField.OnConfirmed += ConfirmModify;
@@ -470,7 +477,8 @@ namespace BansheeEditor
 
                     textureField.OnChanged += x =>
                     {
-                        state.Texture = x as SpriteTexture;
+                        SpriteTexture texture = Resources.Load<SpriteTexture>(x);
+                        state.Texture = texture;
 
                         if (OnChanged != null)
                             OnChanged(state);

+ 12 - 4
MBansheeEditor/Inspectors/MaterialInspector.cs

@@ -23,7 +23,9 @@ namespace BansheeEditor
             shaderField.Value = material.Shader;
             shaderField.OnChanged += (x) =>
             {
-                material.Shader = x as Shader;
+                Shader shader = Resources.Load<Shader>(x);
+
+                material.Shader = shader;
                 RebuildParamGUI(material);
             };
 
@@ -546,21 +548,27 @@ namespace BansheeEditor
                 case ShaderParameterType.Texture2D:
                     guiElem.OnChanged += (x) =>
                     {
-                        material.SetTexture2D(shaderParam.Name, x as Texture2D);
+                        Texture2D texture = Resources.Load<Texture2D>(x);
+
+                        material.SetTexture2D(shaderParam.Name, texture);
                         EditorApplication.SetDirty(material);
                     };
                     break;
                 case ShaderParameterType.Texture3D:
                     guiElem.OnChanged += (x) =>
                     {
-                        material.SetTexture3D(shaderParam.Name, x as Texture3D);
+                        Texture3D texture = Resources.Load<Texture3D>(x);
+
+                        material.SetTexture3D(shaderParam.Name, texture);
                         EditorApplication.SetDirty(material);
                     };
                     break;
                 case ShaderParameterType.TextureCube:
                     guiElem.OnChanged += (x) =>
                     {
-                        material.SetTextureCube(shaderParam.Name, x as TextureCube);
+                        TextureCube texture = Resources.Load<TextureCube>(x);
+
+                        material.SetTextureCube(shaderParam.Name, texture);
                         EditorApplication.SetDirty(material);
                     };
                     break;

+ 6 - 2
MBansheeEditor/Inspectors/RenderableInspector.cs

@@ -124,7 +124,9 @@ namespace BansheeEditor
 
             meshField.OnChanged += x =>
             {
-                renderable.Mesh = x as Mesh;
+                Mesh mesh = Resources.Load<Mesh>(x);
+                renderable.Mesh = mesh;
+
                 MarkAsModified();
                 ConfirmModify();
             };
@@ -206,7 +208,9 @@ namespace BansheeEditor
 
                 materialField.OnChanged += x =>
                 {
-                    SetValue(x);
+                    Material material = Resources.Load<Material>(x);
+
+                    SetValue(material);
                     MarkAsModified();
                     ConfirmModify();
                 };

+ 2 - 1
MBansheeEditor/Inspectors/SpriteTextureInspector.cs

@@ -22,7 +22,8 @@ namespace BansheeEditor
 
             textureField.OnChanged += (x) =>
             {
-                spriteTexture.Texture = x as Texture2D;
+                Texture2D texture = Resources.Load<Texture2D>(x);
+                spriteTexture.Texture = texture;
                 EditorApplication.SetDirty(spriteTexture);
             };
 

+ 34 - 9
MBansheeEngine/ResourceRef.cs

@@ -4,14 +4,14 @@ using System.Runtime.CompilerServices;
 namespace BansheeEngine
 {
     /// <summary>
-    /// Allows you to store a reference to a resource without needing to have that resource loaded.
+    /// Common class for all resource references. <see cref="ResourceRef{T}"/>
     /// </summary>
-    public class ResourceRef<T> : ScriptObject where T : Resource
+    public class ResourceRefBase : ScriptObject
     {
         /// <summary>
         /// Constructor for internal use only.
         /// </summary>
-        private ResourceRef()
+        protected ResourceRefBase()
         { }
 
         /// <summary>
@@ -22,6 +22,37 @@ namespace BansheeEngine
             get { return Internal_IsLoaded(mCachedPtr); }
         }
 
+        /// <inheritdoc/>
+        public override bool Equals(object other)
+        {
+            if (!(other is ResourceRefBase))
+                return false;
+
+            ResourceRefBase otherRef = (ResourceRefBase)other;
+            return Internal_GetUUID(mCachedPtr).Equals(Internal_GetUUID(otherRef.mCachedPtr));
+        }
+
+        /// <inheritdoc/>
+        public override int GetHashCode()
+        {
+            return Internal_GetUUID(mCachedPtr).GetHashCode();
+        }
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern bool Internal_IsLoaded(IntPtr thisPtr);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        protected static extern Resource Internal_GetResource(IntPtr thisPtr);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern string Internal_GetUUID(IntPtr thisPtr);
+    }
+
+    /// <summary>
+    /// Allows you to store a reference to a resource without needing to have that resource loaded.
+    /// </summary>
+    public class ResourceRef<T> : ResourceRefBase where T : Resource
+    {
         /// <summary>
         /// Retrieves the referenced resource. This will load the resources if it is not already loaded.
         /// </summary>
@@ -31,11 +62,5 @@ namespace BansheeEngine
         {
             return (T)Internal_GetResource(mCachedPtr);
         }
-
-        [MethodImpl(MethodImplOptions.InternalCall)]
-        private static extern bool Internal_IsLoaded(IntPtr thisPtr);
-
-        [MethodImpl(MethodImplOptions.InternalCall)]
-        private static extern Resource Internal_GetResource(IntPtr thisPtr);
     }
 }

+ 17 - 1
MBansheeEngine/Resources.cs

@@ -11,7 +11,7 @@ namespace BansheeEngine
         /// <summary>
         /// Loads a resource at the specified path. If running outside of the editor you must make sure to mark that 
         /// the resource gets included in the build. If running inside the editor this has similar functionality as
-        /// if loading using the project library.
+        /// if loading using the project library. If resource is already loaded an existing instance is returned.
         /// </summary>
         /// <typeparam name="T">Type of the resource.</typeparam>
         /// <param name="path">Path of the resource, relative to game directory. If running from editor this will be
@@ -22,6 +22,19 @@ namespace BansheeEngine
             return (T)Internal_Load(path);
         }
 
+        /// <summary>
+        /// Loads a resource referenced by the provided reference. If running outside of the editor you must make sure 
+        /// to mark that the resource gets included in the build. If running inside the editor this has similar functionality 
+        /// as if loading using the project library. If resource is already loaded an existing instance is returned.
+        /// </summary>
+        /// <typeparam name="T">Type of the resource.</typeparam>
+        /// <param name="reference">Reference to the resource to load.</param>
+        /// <returns>Loaded resource, or null if resource cannot be found.</returns>
+        public static T Load<T>(ResourceRefBase reference) where T : Resource
+        {
+            return (T)Internal_LoadRef(reference);
+        }
+
         /// <summary>
         /// Unloads all resources that are no longer referenced. Usually the system keeps resources in memory even if
         /// they are no longer referenced to avoid constant loading/unloading if resource is often passed around.
@@ -34,6 +47,9 @@ namespace BansheeEngine
         [MethodImpl(MethodImplOptions.InternalCall)]
         private static extern Resource Internal_Load(string path);
 
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern Resource Internal_LoadRef(ResourceRefBase reference);
+
         [MethodImpl(MethodImplOptions.InternalCall)]
         private static extern void Internal_UnloadUnused();
     }

+ 12 - 2
SBansheeEditor/Include/BsGUIResourceField.h

@@ -172,6 +172,16 @@ namespace BansheeEngine
 		 */
 		void setValue(const HResource& value);
 
+		/**
+		 * @brief	Returns a weak reference to the texture referenced by the field, if any.
+		 */
+		WeakResourceHandle<Resource> getValueWeak() const;
+
+		/**
+		 * @brief	Sets a weak reference to the texture referenced by the field.
+		 */
+		void setValueWeak(const WeakResourceHandle<Resource>& value);
+
 		/**
 		 * @brief	Returns the resource referenced by the field. Returns
 		 *			empty string with no resource is referenced.
@@ -195,9 +205,9 @@ namespace BansheeEngine
 
 		/**
 		 * @brief	Triggered whenever the referenced resource changes. Provides
-		 *			the UUID of the resource, or empty string if no resource is referenced.
+		 *			a weak handle of the resource, or empty handle if no resource is referenced.
 		 */
-		Event<void(const String&)> onValueChanged;
+		Event<void(const WeakResourceHandle<Resource>&)> onValueChanged;
 	private:
 		virtual ~GUIResourceField();
 

+ 14 - 3
SBansheeEditor/Include/BsGUITextureField.h

@@ -144,7 +144,8 @@ namespace BansheeEngine
 			UINT32 labelWidth, const String& style, const GUIDimensions& dimensions, bool withLabel);
 
 		/**
-		 * @brief	Returns the texture referenced by the field, if any.
+		 * @brief	Returns the texture referenced by the field, if any. This will load the texture if it is not already
+		 * 			loaded.
 		 */
 		HTexture getValue() const;
 
@@ -153,6 +154,16 @@ namespace BansheeEngine
 		 */
 		void setValue(const HTexture& value);
 
+		/**
+		 * @brief	Returns a weak reference to the texture referenced by the field, if any.
+		 */
+		WeakResourceHandle<Texture> getValueWeak() const;
+
+		/**
+		 * @brief	Sets a weak reference to the texture referenced by the field.
+		 */
+		void setValueWeak(const WeakResourceHandle<Texture>& value);
+
 		/**
 		 * @brief	Returns the texture referenced by the field. Returns
 		 *			empty string with no texture is referenced.
@@ -176,9 +187,9 @@ namespace BansheeEngine
 
 		/**
 		 * @brief	Triggered whenever the referenced texture changes. Provides
-		 *			the UUID of the resource, or empty string if no texture is referenced.
+		 *			a weak handle to the resource, or empty handle if no texture is referenced.
 		 */
-		Event<void(const String&)> onValueChanged;
+		Event<void(const WeakResourceHandle<Texture>&)> onValueChanged;
 	private:
 		virtual ~GUITextureField();
 

+ 4 - 2
SBansheeEditor/Include/BsScriptGUIResourceField.h

@@ -18,9 +18,9 @@ namespace BansheeEngine
 		 * @brief	Triggered when the value in the native resource field changes.
 		 *
 		 * @param	instance	Managed GUIResourceField instance.
-		 * @param	newValue	UUID of the newly selected resource.
+		 * @param	newHandle	Weak handle of the newly selected resource.
 		 */
-		static void onChanged(MonoObject* instance, const String& newUUID);
+		static void onChanged(MonoObject* instance, const WeakResourceHandle<Resource>& newHandle);
 
 		/**
 		 * @brief	Retrieves a managed instance of the specified native resource.
@@ -38,6 +38,8 @@ namespace BansheeEngine
 
 		static void internal_getValue(ScriptGUIResourceField* nativeInstance, MonoObject** output);
 		static void internal_setValue(ScriptGUIResourceField* nativeInstance, MonoObject* value);
+		static void internal_getValueRef(ScriptGUIResourceField* nativeInstance, MonoObject** output);
+		static void internal_setValueRef(ScriptGUIResourceField* nativeInstance, MonoObject* value);
 		static void internal_setTint(ScriptGUIResourceField* nativeInstance, Color color);
 
 		typedef void(__stdcall *OnChangedThunkDef) (MonoObject*, MonoObject*, MonoException**);

+ 4 - 2
SBansheeEditor/Include/BsScriptGUITextureField.h

@@ -18,9 +18,9 @@ namespace BansheeEngine
 		 * @brief	Triggered when the value in the native texture field changes.
 		 *
 		 * @param	instance	Managed GUITextureField instance.
-		 * @param	newValue	UUID of thew new texture.
+		 * @param	newHandle	Handle of the new texture.
 		 */
-		static void onChanged(MonoObject* instance, const String& newUUID);
+		static void onChanged(MonoObject* instance, const WeakResourceHandle<Texture>& newHandle);
 
 		/**
 		 * @brief	Retrieves a managed instance of the specified native texture.
@@ -39,6 +39,8 @@ namespace BansheeEngine
 
 		static void internal_getValue(ScriptGUITextureField* nativeInstance, MonoObject** output);
 		static void internal_setValue(ScriptGUITextureField* nativeInstance, MonoObject* value);
+		static void internal_getValueRef(ScriptGUITextureField* nativeInstance, MonoObject** output);
+		static void internal_setValueRef(ScriptGUITextureField* nativeInstance, MonoObject* value);
 		static void internal_setTint(ScriptGUITextureField* nativeInstance, Color color);
 
 		typedef void(__stdcall *OnChangedThunkDef) (MonoObject*, MonoObject*, MonoException**);

+ 2 - 2
SBansheeEditor/Include/BsScriptPlatformInfo.h

@@ -48,7 +48,7 @@ namespace BansheeEngine
 		static MonoString* internal_GetDefines(ScriptPlatformInfoBase* thisPtr);
 		static void internal_SetDefines(ScriptPlatformInfoBase* thisPtr, MonoString* value);
 		static MonoObject* internal_GetMainScene(ScriptPlatformInfoBase* thisPtr);
-		static void internal_SetMainScene(ScriptPlatformInfoBase* thisPtr, ScriptPrefab* prefabPtr);
+		static void internal_SetMainScene(ScriptPlatformInfoBase* thisPtr, ScriptResourceRefBase* prefabRef);
 		static bool internal_GetFullscreen(ScriptPlatformInfoBase* thisPtr);
 		static void internal_SetFullscreen(ScriptPlatformInfoBase* thisPtr, bool fullscreen);
 		static void internal_GetResolution(ScriptPlatformInfoBase* thisPtr, UINT32* width, UINT32* height);
@@ -83,7 +83,7 @@ namespace BansheeEngine
 		/* 								CLR HOOKS						   		*/
 		/************************************************************************/
 		static MonoObject* internal_GetIcon(ScriptWinPlatformInfo* thisPtr);
-		static void internal_SetIcon(ScriptWinPlatformInfo* thisPtr, ScriptTexture2D* texturePtr);
+		static void internal_SetIcon(ScriptWinPlatformInfo* thisPtr, ScriptResourceRefBase* textureRef);
 		static MonoString* internal_GetTitleText(ScriptWinPlatformInfo* thisPtr);
 		static void internal_SetTitleText(ScriptWinPlatformInfo* thisPtr, MonoString* text);
 	};

+ 34 - 1
SBansheeEditor/Source/BsGUIResourceField.cpp

@@ -193,6 +193,36 @@ namespace BansheeEngine
 			setUUID("", false);
 	}
 
+	WeakResourceHandle<Resource> GUIResourceField::getValueWeak() const
+	{
+		if (!mUUID.empty())
+			return Resources::instance()._getResourceHandle(mUUID).getWeak();
+
+		return WeakResourceHandle<Resource>();
+	}
+
+	void GUIResourceField::setValueWeak(const WeakResourceHandle<Resource>& value)
+	{
+		if (value)
+		{
+			Path resPath = gProjectLibrary().uuidToPath(value.getUUID());
+			if (!resPath.isEmpty())
+				setUUID(value.getUUID(), false);
+			else // A non-project library resource
+			{
+				if (mUUID == value.getUUID())
+					return;
+
+				mUUID = value.getUUID();
+
+				WString title = value->getName() + L" (" + toWString(mType) + L")";
+				mDropButton->setContent(GUIContent(HEString(title)));
+			}
+		}
+		else
+			setUUID("", false);
+	}
+
 	void GUIResourceField::setUUID(const String& uuid, bool triggerEvent)
 	{ 
 		if (mUUID == uuid)
@@ -210,7 +240,10 @@ namespace BansheeEngine
 			mDropButton->setContent(GUIContent(HEString(L"None (" + toWString(mType) + L")")));
 
 		if (triggerEvent)
-			onValueChanged(mUUID);
+		{
+			HResource handle = gResources()._getResourceHandle(mUUID);
+			onValueChanged(handle.getWeak());
+		}
 	}
 
 	void GUIResourceField::setTint(const Color& color)

+ 15 - 1
SBansheeEditor/Source/BsGUITextureField.cpp

@@ -178,6 +178,20 @@ namespace BansheeEngine
 			setUUID("", false);
 	}
 
+	WeakResourceHandle<Texture> GUITextureField::getValueWeak() const
+	{
+		HResource handle = Resources::instance()._getResourceHandle(mUUID);
+		return static_resource_cast<Texture>(handle.getWeak());
+	}
+
+	void GUITextureField::setValueWeak(const WeakResourceHandle<Texture>& value)
+	{
+		if (value)
+			setUUID(value.getUUID(), false);
+		else
+			setUUID("", false);
+	}
+
 	void GUITextureField::setUUID(const String& uuid, bool triggerEvent)
 	{
 		if (mUUID == uuid)
@@ -204,7 +218,7 @@ namespace BansheeEngine
 		}
 
 		if (triggerEvent)
-			onValueChanged(mUUID);
+			onValueChanged(texture.getWeak());
 	}
 
 	void GUITextureField::setTint(const Color& color)

+ 3 - 3
SBansheeEditor/Source/BsScriptBrowseDialog.cpp

@@ -28,7 +28,7 @@ namespace BansheeEngine
 
 			for (UINT32 i = 0; i < (UINT32)paths.size(); i++)
 			{
-				MonoString* monoString = MonoUtil::wstringToMono(MonoManager::instance().getDomain(), paths[i].toWString());
+				MonoString* monoString = MonoUtil::wstringToMono(paths[i].toWString());
 
 				void* elemAddr = mono_array_addr_with_size(pathArray, sizeof(MonoString*), i);
 				memcpy(elemAddr, &monoString, sizeof(MonoString*));
@@ -54,7 +54,7 @@ namespace BansheeEngine
 		if (PlatformUtility::openBrowseDialog(type, defaultFolderNative, L"", paths))
 		{
 			if (paths.size() > 0)
-				*outPath = MonoUtil::wstringToMono(MonoManager::instance().getDomain(), paths[0].toWString());
+				*outPath = MonoUtil::wstringToMono(paths[0].toWString());
 			else
 				*outPath = nullptr;
 
@@ -78,7 +78,7 @@ namespace BansheeEngine
 		if (PlatformUtility::openBrowseDialog(type, defaultFolderNative, filterListNative, paths))
 		{
 			if (paths.size() > 0)
-				*outPath = MonoUtil::wstringToMono(MonoManager::instance().getDomain(), paths[0].toWString());
+				*outPath = MonoUtil::wstringToMono(paths[0].toWString());
 			else
 				*outPath = nullptr;
 

+ 8 - 8
SBansheeEditor/Source/BsScriptBuildManager.cpp

@@ -81,19 +81,19 @@ namespace BansheeEngine
 		ScriptArray outArray = ScriptArray::create<WString>((UINT32)frameworkAssemblies.size());
 		UINT32 idx = 0;
 		for (auto& assemblyName : frameworkAssemblies)
-			outArray.set(idx++, MonoUtil::wstringToMono(MonoManager::instance().getDomain(), assemblyName));
+			outArray.set(idx++, MonoUtil::wstringToMono(assemblyName));
 
 		return outArray.getInternal();
 	}
 
 	MonoString* ScriptBuildManager::internal_GetMainExecutable(PlatformType type)
 	{
-		return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), BuildManager::instance().getMainExecutable(type).toWString());
+		return MonoUtil::wstringToMono(BuildManager::instance().getMainExecutable(type).toWString());
 	}
 
 	MonoString* ScriptBuildManager::internal_GetDefines(PlatformType type)
 	{
-		return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), BuildManager::instance().getDefines(type));
+		return MonoUtil::wstringToMono(BuildManager::instance().getDefines(type));
 	}
 
 	MonoArray* ScriptBuildManager::internal_GetNativeBinaries(PlatformType type)
@@ -104,7 +104,7 @@ namespace BansheeEngine
 		ScriptArray outArray = ScriptArray::create<WString>(numEntries);
 		for (UINT32 i = 0; i < numEntries; i++)
 		{
-			outArray.set(i, MonoUtil::wstringToMono(MonoManager::instance().getDomain(), paths[i].toWString()));
+			outArray.set(i, MonoUtil::wstringToMono(paths[i].toWString()));
 		}
 
 		return outArray.getInternal();
@@ -158,7 +158,7 @@ namespace BansheeEngine
 			path = BuildManager::instance().getBuildFolder(nativeFolderType, platform);
 		}
 
-		return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), path.toWString());
+		return MonoUtil::wstringToMono(path.toWString());
 	}
 
 	void ScriptBuildManager::internal_InjectIcons(MonoString* filePath, ScriptPlatformInfo* info)
@@ -194,7 +194,7 @@ namespace BansheeEngine
 				{ 256 } 
 			};
 
-			HTexture icon = winPlatformInfo->icon;
+			HTexture icon = gResources().load(winPlatformInfo->icon);
 			if (icon.isLoaded())
 			{
 				auto& texProps = icon->getProperties();
@@ -363,7 +363,7 @@ namespace BansheeEngine
 		{
 			SPtr<WinPlatformInfo> winPlatformInfo = std::static_pointer_cast<WinPlatformInfo>(platformInfo);
 			
-			HTexture icon = winPlatformInfo->icon;
+			HTexture icon = gResources().load(winPlatformInfo->icon);
 			if (icon != nullptr)
 				gResources().save(icon, destIconFile, true);
 		}
@@ -391,7 +391,7 @@ namespace BansheeEngine
 		if (platformInfo != nullptr)
 		{
 			gameSettings = bs_shared_ptr_new<GameSettings>();
-			gameSettings->mainSceneUUID = platformInfo->mainScene.getUUID();
+			gameSettings->mainScene = platformInfo->mainScene;
 			gameSettings->fullscreen = platformInfo->fullscreen;
 			gameSettings->resolutionWidth = platformInfo->windowedWidth;
 			gameSettings->resolutionWidth = platformInfo->windowedHeight;

+ 2 - 2
SBansheeEditor/Source/BsScriptDragDropManager.cpp

@@ -160,7 +160,7 @@ namespace BansheeEngine
 		UINT32 idx = 0;
 		for (auto& path : paths)
 		{
-			MonoString* managedPath = MonoUtil::wstringToMono(MonoManager::instance().getDomain(), path.toWString());
+			MonoString* managedPath = MonoUtil::wstringToMono(path.toWString());
 
 			arrStrings.set(idx, managedPath);
 			idx++;
@@ -196,7 +196,7 @@ namespace BansheeEngine
 		UINT32 idx = 0;
 		for (auto& path : instance->mPaths)
 		{
-			MonoString* managedPath = MonoUtil::wstringToMono(MonoManager::instance().getDomain(), path.toWString());
+			MonoString* managedPath = MonoUtil::wstringToMono(path.toWString());
 
 			arrStrings.set(idx, managedPath);
 			idx++;

+ 12 - 12
SBansheeEditor/Source/BsScriptEditorApplication.cpp

@@ -136,14 +136,14 @@ namespace BansheeEngine
 	{
 		Path projectPath = gEditorApplication().getProjectPath();
 
-		return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), projectPath.toWString());
+		return MonoUtil::wstringToMono(projectPath.toWString());
 	}
 
 	MonoString* ScriptEditorApplication::internal_GetProjectName()
 	{
 		WString projectName = gEditorApplication().getProjectName();
 
-		return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), projectName);
+		return MonoUtil::wstringToMono(projectName);
 	}
 
 	bool ScriptEditorApplication::internal_GetProjectLoaded()
@@ -155,7 +155,7 @@ namespace BansheeEngine
 	{
 		Path compilerPath = MonoManager::instance().getCompilerPath();
 
-		return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), compilerPath.toWString());
+		return MonoUtil::wstringToMono(compilerPath.toWString());
 	}
 
 	MonoString* ScriptEditorApplication::internal_GetBuiltinReleaseAssemblyPath()
@@ -163,7 +163,7 @@ namespace BansheeEngine
 		Path releaseAssemblyFolder = FileSystem::getWorkingDirectoryPath();
 		releaseAssemblyFolder.append(Paths::getReleaseAssemblyPath());
 
-		return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), releaseAssemblyFolder.toWString());
+		return MonoUtil::wstringToMono(releaseAssemblyFolder.toWString());
 	}
 
 	MonoString* ScriptEditorApplication::internal_GetBuiltinDebugAssemblyPath()
@@ -171,14 +171,14 @@ namespace BansheeEngine
 		Path debugAssemblyFolder = FileSystem::getWorkingDirectoryPath();
 		debugAssemblyFolder.append(Paths::getDebugAssemblyPath());
 
-		return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), debugAssemblyFolder.toWString());
+		return MonoUtil::wstringToMono(debugAssemblyFolder.toWString());
 	}
 
 	MonoString* ScriptEditorApplication::internal_GetScriptAssemblyPath()
 	{
 		Path assemblyFolder = gEditorApplication().getScriptAssemblyFolder();
 
-		return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), assemblyFolder.toWString());
+		return MonoUtil::wstringToMono(assemblyFolder.toWString());
 	}
 
 	MonoString* ScriptEditorApplication::internal_GetFrameworkAssemblyPath()
@@ -186,27 +186,27 @@ namespace BansheeEngine
 		Path assemblyFolder = MonoManager::instance().getFrameworkAssembliesFolder();
 		assemblyFolder.makeAbsolute(FileSystem::getWorkingDirectoryPath());
 
-		return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), assemblyFolder.toWString());
+		return MonoUtil::wstringToMono(assemblyFolder.toWString());
 	}
 
 	MonoString* ScriptEditorApplication::internal_GetEngineAssemblyName()
 	{
-		return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), toWString(ENGINE_ASSEMBLY) + L".dll");
+		return MonoUtil::wstringToMono(toWString(ENGINE_ASSEMBLY) + L".dll");
 	}
 
 	MonoString* ScriptEditorApplication::internal_GetEditorAssemblyName()
 	{
-		return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), toWString(EDITOR_ASSEMBLY) + L".dll");
+		return MonoUtil::wstringToMono(toWString(EDITOR_ASSEMBLY) + L".dll");
 	}
 
 	MonoString* ScriptEditorApplication::internal_GetScriptGameAssemblyName()
 	{
-		return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), toWString(SCRIPT_GAME_ASSEMBLY) + L".dll");
+		return MonoUtil::wstringToMono(toWString(SCRIPT_GAME_ASSEMBLY) + L".dll");
 	}
 
 	MonoString* ScriptEditorApplication::internal_GetScriptEditorAssemblyName()
 	{
-		return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), toWString(SCRIPT_EDITOR_ASSEMBLY) + L".dll");
+		return MonoUtil::wstringToMono(toWString(SCRIPT_EDITOR_ASSEMBLY) + L".dll");
 	}
 
 	MonoString* ScriptEditorApplication::internal_SaveScene(MonoString* path)
@@ -236,7 +236,7 @@ namespace BansheeEngine
 			gProjectLibrary().createEntry(scene, nativePath);
 		}
 
-		return MonoUtil::stringToMono(MonoManager::instance().getDomain(), scene.getUUID());
+		return MonoUtil::stringToMono(scene.getUUID());
 	}
 
 	bool ScriptEditorApplication::internal_IsValidProject(MonoString* path)

+ 2 - 2
SBansheeEditor/Source/BsScriptEditorBuiltin.cpp

@@ -43,14 +43,14 @@ namespace BansheeEngine
 	{
 		WString code = BuiltinEditorResources::instance().getEmptyShaderCode();
 
-		return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), code);
+		return MonoUtil::wstringToMono(code);
 	}
 
 	MonoString* ScriptEditorBuiltin::internal_GetEmptyCSScriptCode()
 	{
 		WString code = BuiltinEditorResources::instance().getEmptyCSScriptCode();
 
-		return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), code);
+		return MonoUtil::wstringToMono(code);
 	}
 
 	MonoObject* ScriptEditorBuiltin::internal_GetToolbarIcon(ToolbarIcon icon)

+ 3 - 3
SBansheeEditor/Source/BsScriptEditorSettings.cpp

@@ -150,7 +150,7 @@ namespace BansheeEngine
 	MonoString* ScriptEditorSettings::internal_GetLastOpenProject()
 	{
 		EditorSettingsPtr settings = gEditorApplication().getEditorSettings();
-		return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), settings->getLastOpenProject().toWString());
+		return MonoUtil::wstringToMono(settings->getLastOpenProject().toWString());
 	}
 
 	void ScriptEditorSettings::internal_SetLastOpenProject(MonoString* value)
@@ -183,7 +183,7 @@ namespace BansheeEngine
 		for (UINT32 i = 0; i < numEntries; i++)
 		{
 			WString projectPath = recentProjects[i].path.toWString();
-			MonoString* monoPath = MonoUtil::wstringToMono(MonoManager::instance().getDomain(), projectPath);
+			MonoString* monoPath = MonoUtil::wstringToMono(projectPath);
 
 			outputPaths.set(i, monoPath);
 			outputTimeStamps.set(i, recentProjects[i].accessTimestamp);
@@ -275,7 +275,7 @@ namespace BansheeEngine
 		EditorSettingsPtr settings = gEditorApplication().getEditorSettings();
 		WString nativeValue = settings->getString(nativeName, nativeDefaultValue);
 
-		return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), nativeValue);
+		return MonoUtil::wstringToMono(nativeValue);
 	}
 
 	bool ScriptEditorSettings::internal_HasKey(MonoString* name)

+ 5 - 5
SBansheeEditor/Source/BsScriptFolderMonitor.cpp

@@ -69,26 +69,26 @@ namespace BansheeEngine
 
 	void ScriptFolderMonitor::onMonitorFileModified(const Path& path)
 	{
-		MonoString* monoPath = MonoUtil::wstringToMono(MonoManager::instance().getDomain(), path.toWString());
+		MonoString* monoPath = MonoUtil::wstringToMono(path.toWString());
 		MonoUtil::invokeThunk(OnModifiedThunk, getManagedInstance(), monoPath);
 	}
 
 	void ScriptFolderMonitor::onMonitorFileAdded(const Path& path)
 	{
-		MonoString* monoPath = MonoUtil::wstringToMono(MonoManager::instance().getDomain(), path.toWString());
+		MonoString* monoPath = MonoUtil::wstringToMono(path.toWString());
 		MonoUtil::invokeThunk(OnAddedThunk, getManagedInstance(), monoPath);
 	}
 
 	void ScriptFolderMonitor::onMonitorFileRemoved(const Path& path)
 	{
-		MonoString* monoPath = MonoUtil::wstringToMono(MonoManager::instance().getDomain(), path.toWString());
+		MonoString* monoPath = MonoUtil::wstringToMono(path.toWString());
 		MonoUtil::invokeThunk(OnRemovedThunk, getManagedInstance(), monoPath);
 	}
 
 	void ScriptFolderMonitor::onMonitorFileRenamed(const Path& from, const Path& to)
 	{
-		MonoString* monoPathFrom = MonoUtil::wstringToMono(MonoManager::instance().getDomain(), from.toWString());
-		MonoString* monoPathTo = MonoUtil::wstringToMono(MonoManager::instance().getDomain(), to.toWString());
+		MonoString* monoPathFrom = MonoUtil::wstringToMono(from.toWString());
+		MonoString* monoPathTo = MonoUtil::wstringToMono(to.toWString());
 
 		MonoUtil::invokeThunk(OnRenamedThunk, getManagedInstance(), monoPathFrom, monoPathTo);
 	}

+ 26 - 4
SBansheeEditor/Source/BsScriptGUIResourceField.cpp

@@ -16,6 +16,7 @@
 #include "BsScriptGUIContent.h"
 #include "BsScriptResource.h"
 #include "BsScriptResourceManager.h"
+#include "BsScriptResourceRef.h"
 #include "BsResources.h"
 
 using namespace std::placeholders;
@@ -35,6 +36,8 @@ namespace BansheeEngine
 		metaData.scriptClass->addInternalCall("Internal_CreateInstance", &ScriptGUIResourceField::internal_createInstance);
 		metaData.scriptClass->addInternalCall("Internal_GetValue", &ScriptGUIResourceField::internal_getValue);
 		metaData.scriptClass->addInternalCall("Internal_SetValue", &ScriptGUIResourceField::internal_setValue);
+		metaData.scriptClass->addInternalCall("Internal_GetValueRef", &ScriptGUIResourceField::internal_getValueRef);
+		metaData.scriptClass->addInternalCall("Internal_SetValueRef", &ScriptGUIResourceField::internal_setValueRef);
 		metaData.scriptClass->addInternalCall("Internal_SetTint", &ScriptGUIResourceField::internal_setTint);
 
 		onChangedThunk = (OnChangedThunkDef)metaData.scriptClass->getMethod("DoOnChanged", 1)->getThunk();
@@ -95,17 +98,36 @@ namespace BansheeEngine
 		}
 	}
 
+	void ScriptGUIResourceField::internal_getValueRef(ScriptGUIResourceField* nativeInstance, MonoObject** output)
+	{
+		GUIResourceField* resourceField = static_cast<GUIResourceField*>(nativeInstance->getGUIElement());
+
+		WeakResourceHandle<Resource> resource = resourceField->getValueWeak();
+		*output = ScriptResourceRef::create(resource);
+	}
+
+	void ScriptGUIResourceField::internal_setValueRef(ScriptGUIResourceField* nativeInstance, MonoObject* value)
+	{
+		GUIResourceField* resourceField = static_cast<GUIResourceField*>(nativeInstance->getGUIElement());
+
+		if (value == nullptr)
+			resourceField->setValue(HTexture());
+		else
+		{
+			ScriptResourceRefBase* scriptTexture = ScriptResourceRefBase::toNative(value);
+			resourceField->setValueWeak(static_resource_cast<Texture>(scriptTexture->getHandle()));
+		}
+	}
+
 	void ScriptGUIResourceField::internal_setTint(ScriptGUIResourceField* nativeInstance, Color color)
 	{
 		GUIResourceField* resourceField = (GUIResourceField*)nativeInstance->getGUIElement();
 		resourceField->setTint(color);
 	}
 
-	void ScriptGUIResourceField::onChanged(MonoObject* instance, const String& newUUID)
+	void ScriptGUIResourceField::onChanged(MonoObject* instance, const WeakResourceHandle<Resource>& newHandle)
 	{
-		HResource resource = gResources().loadFromUUID(newUUID);
-
-		MonoObject* managedObj = nativeToManagedResource(resource);
+		MonoObject* managedObj = ScriptResourceRef::create(newHandle);
 		MonoUtil::invokeThunk(onChangedThunk, instance, managedObj);
 	}
 

+ 3 - 3
SBansheeEditor/Source/BsScriptGUITextField.cpp

@@ -71,10 +71,10 @@ namespace BansheeEngine
 	void ScriptGUITextField::internal_getValue(ScriptGUITextField* nativeInstance, MonoString** output)
 	{
 		if (nativeInstance->isDestroyed())
-			*output = MonoUtil::wstringToMono(MonoManager::instance().getDomain(), StringUtil::WBLANK);
+			*output = MonoUtil::wstringToMono(StringUtil::WBLANK);
 
 		GUITextField* field = static_cast<GUITextField*>(nativeInstance->getGUIElement());
-		*output = MonoUtil::wstringToMono(MonoManager::instance().getDomain(), field->getValue());
+		*output = MonoUtil::wstringToMono(field->getValue());
 	}
 
 	void ScriptGUITextField::internal_setValue(ScriptGUITextField* nativeInstance, MonoString* value)
@@ -106,7 +106,7 @@ namespace BansheeEngine
 
 	void ScriptGUITextField::onChanged(MonoObject* instance, const WString& newValue)
 	{
-		MonoString* monoNewValue = MonoUtil::wstringToMono(MonoManager::instance().getDomain(), newValue);
+		MonoString* monoNewValue = MonoUtil::wstringToMono(newValue);
 		MonoUtil::invokeThunk(onChangedThunk, instance, monoNewValue);
 	}
 

+ 27 - 4
SBansheeEditor/Source/BsScriptGUITextureField.cpp

@@ -16,6 +16,7 @@
 #include "BsScriptGUIContent.h"
 #include "BsScriptTexture.h"
 #include "BsScriptResourceManager.h"
+#include "BsScriptResourceRef.h"
 #include "BsResources.h"
 
 using namespace std::placeholders;
@@ -35,6 +36,8 @@ namespace BansheeEngine
 		metaData.scriptClass->addInternalCall("Internal_CreateInstance", &ScriptGUITextureField::internal_createInstance);
 		metaData.scriptClass->addInternalCall("Internal_GetValue", &ScriptGUITextureField::internal_getValue);
 		metaData.scriptClass->addInternalCall("Internal_SetValue", &ScriptGUITextureField::internal_setValue);
+		metaData.scriptClass->addInternalCall("Internal_GetValueRef", &ScriptGUITextureField::internal_getValueRef);
+		metaData.scriptClass->addInternalCall("Internal_SetValueRef", &ScriptGUITextureField::internal_setValueRef);
 		metaData.scriptClass->addInternalCall("Internal_SetTint", &ScriptGUITextureField::internal_setTint);
 
 		onChangedThunk = (OnChangedThunkDef)metaData.scriptClass->getMethod("Internal_DoOnChanged", 1)->getThunk();
@@ -88,17 +91,37 @@ namespace BansheeEngine
 		}
 	}
 
+	void ScriptGUITextureField::internal_getValueRef(ScriptGUITextureField* nativeInstance, MonoObject** output)
+	{
+		GUITextureField* textureField = static_cast<GUITextureField*>(nativeInstance->getGUIElement());
+
+		WeakResourceHandle<Texture> resource = textureField->getValueWeak();
+		*output = ScriptResourceRef::create(resource);
+	}
+
+	void ScriptGUITextureField::internal_setValueRef(ScriptGUITextureField* nativeInstance, MonoObject* value)
+	{
+		GUITextureField* textureField = static_cast<GUITextureField*>(nativeInstance->getGUIElement());
+
+		if (value == nullptr)
+			textureField->setValue(HTexture());
+		else
+		{
+			ScriptResourceRefBase* scriptTexture = ScriptResourceRefBase::toNative(value);
+			textureField->setValueWeak(static_resource_cast<Texture>(scriptTexture->getHandle()));
+		}
+	}
+
 	void ScriptGUITextureField::internal_setTint(ScriptGUITextureField* nativeInstance, Color color)
 	{
 		GUITextureField* textureField = (GUITextureField*)nativeInstance->getGUIElement();
 		textureField->setTint(color);
 	}
 
-	void ScriptGUITextureField::onChanged(MonoObject* instance, const String& newUUID)
+	void ScriptGUITextureField::onChanged(MonoObject* instance, const WeakResourceHandle<Texture>& newHandle)
 	{
-		HTexture texture = static_resource_cast<Texture>(gResources().loadFromUUID(newUUID));
-
-		MonoObject* managedObj = nativeToManagedResource(texture);
+		// TODO - Always returning a 2D texture, will not work for 3D/cube textures
+		MonoObject* managedObj = ScriptResourceRef::create(newHandle);
 		MonoUtil::invokeThunk(onChangedThunk, instance, managedObj);
 	}
 

+ 15 - 24
SBansheeEditor/Source/BsScriptPlatformInfo.cpp

@@ -8,6 +8,7 @@
 #include "BsScriptTexture2D.h"
 #include "BsScriptResourceManager.h"
 #include "BsScriptPrefab.h"
+#include "BsScriptResourceRef.h"
 
 namespace BansheeEngine
 {
@@ -55,7 +56,7 @@ namespace BansheeEngine
 
 	MonoString* ScriptPlatformInfo::internal_GetDefines(ScriptPlatformInfoBase* thisPtr)
 	{
-		return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), thisPtr->getPlatformInfo()->defines);
+		return MonoUtil::wstringToMono(thisPtr->getPlatformInfo()->defines);
 	}
 
 	void ScriptPlatformInfo::internal_SetDefines(ScriptPlatformInfoBase* thisPtr, MonoString* value)
@@ -65,25 +66,20 @@ namespace BansheeEngine
 
 	MonoObject* ScriptPlatformInfo::internal_GetMainScene(ScriptPlatformInfoBase* thisPtr)
 	{
-		HPrefab prefab = thisPtr->getPlatformInfo()->mainScene;
+		WeakResourceHandle<Prefab> prefab = thisPtr->getPlatformInfo()->mainScene;
 		
 		if (prefab != nullptr)
-		{
-			ScriptPrefab* scriptPrefab;
-			ScriptResourceManager::instance().getScriptResource(prefab, &scriptPrefab, true);
-
-			return scriptPrefab->getManagedInstance();
-		}
+			return ScriptResourceRef::create(prefab);
 
 		return nullptr;
 	}
 
-	void ScriptPlatformInfo::internal_SetMainScene(ScriptPlatformInfoBase* thisPtr, ScriptPrefab* prefabPtr)
+	void ScriptPlatformInfo::internal_SetMainScene(ScriptPlatformInfoBase* thisPtr, ScriptResourceRefBase* prefabRef)
 	{
-		HPrefab prefab;
+		WeakResourceHandle<Prefab> prefab;
 
-		if (prefabPtr != nullptr)
-			prefab = prefabPtr->getHandle();
+		if (prefabRef != nullptr)
+			prefab = static_resource_cast<Prefab>(prefabRef->getHandle());
 
 		thisPtr->getPlatformInfo()->mainScene = prefab;
 	}
@@ -150,25 +146,20 @@ namespace BansheeEngine
 
 	MonoObject* ScriptWinPlatformInfo::internal_GetIcon(ScriptWinPlatformInfo* thisPtr)
 	{
-		HTexture icon = thisPtr->getWinPlatformInfo()->icon;
+		WeakResourceHandle<Texture> icon = thisPtr->getWinPlatformInfo()->icon;
 
 		if (icon != nullptr)
-		{
-			ScriptTexture2D* scriptTexture;
-			ScriptResourceManager::instance().getScriptResource(icon, &scriptTexture, true);
-
-			return scriptTexture->getManagedInstance();
-		}
+			return ScriptResourceRef::create(icon, TEX_TYPE_2D);
 
 		return nullptr;
 	}
 
-	void ScriptWinPlatformInfo::internal_SetIcon(ScriptWinPlatformInfo* thisPtr, ScriptTexture2D* texturePtr)
+	void ScriptWinPlatformInfo::internal_SetIcon(ScriptWinPlatformInfo* thisPtr, ScriptResourceRefBase* textureRef)
 	{
-		HTexture icon;
+		WeakResourceHandle<Texture> icon;
 
-		if (texturePtr != nullptr)
-			icon = texturePtr->getHandle();
+		if (textureRef != nullptr)
+			icon = static_resource_cast<Texture>(textureRef->getHandle());
 
 		thisPtr->getWinPlatformInfo()->icon = icon;
 	}
@@ -177,7 +168,7 @@ namespace BansheeEngine
 	{
 		WString titleText = thisPtr->getWinPlatformInfo()->titlebarText;
 
-		return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), titleText);
+		return MonoUtil::wstringToMono(titleText);
 	}
 
 	void ScriptWinPlatformInfo::internal_SetTitleText(ScriptWinPlatformInfo* thisPtr, MonoString* text)

+ 9 - 9
SBansheeEditor/Source/BsScriptProjectLibrary.cpp

@@ -154,7 +154,7 @@ namespace BansheeEngine
 		String nativeUUID = MonoUtil::monoToString(uuid);
 		Path nativePath = gProjectLibrary().uuidToPath(nativeUUID);
 
-		return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), nativePath.toWString());
+		return MonoUtil::wstringToMono(nativePath.toWString());
 	}
 
 	MonoString* ScriptProjectLibrary::internal_GetPath(MonoObject* resource)
@@ -166,7 +166,7 @@ namespace BansheeEngine
 			Path nativePath = gProjectLibrary().uuidToPath(srcResource->getGenericHandle().getUUID());
 			nativePath.getRelative(gProjectLibrary().getResourcesFolder());
 
-			return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), nativePath.toWString());
+			return MonoUtil::wstringToMono(nativePath.toWString());
 		}
 
 		return nullptr;
@@ -245,7 +245,7 @@ namespace BansheeEngine
 
 	MonoString* ScriptProjectLibrary::internal_GetResourceFolder()
 	{
-		return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), gProjectLibrary().getResourcesFolder().toWString());
+		return MonoUtil::wstringToMono(gProjectLibrary().getResourcesFolder().toWString());
 	}
 
 	void ScriptProjectLibrary::internal_SetIncludeInBuild(MonoString* path, bool include)
@@ -275,7 +275,7 @@ namespace BansheeEngine
 		if (relativePath.isAbsolute())
 			relativePath.makeRelative(gProjectLibrary().getResourcesFolder());
 
-		MonoString* pathStr = MonoUtil::wstringToMono(MonoManager::instance().getDomain(), relativePath.toWString());
+		MonoString* pathStr = MonoUtil::wstringToMono(relativePath.toWString());
 		MonoUtil::invokeThunk(OnEntryAddedThunk, pathStr);
 	}
 
@@ -285,7 +285,7 @@ namespace BansheeEngine
 		if (relativePath.isAbsolute())
 			relativePath.makeRelative(gProjectLibrary().getResourcesFolder());
 
-		MonoString* pathStr = MonoUtil::wstringToMono(MonoManager::instance().getDomain(), relativePath.toWString());
+		MonoString* pathStr = MonoUtil::wstringToMono(relativePath.toWString());
 		MonoUtil::invokeThunk(OnEntryRemovedThunk, pathStr);
 	}
 
@@ -295,7 +295,7 @@ namespace BansheeEngine
 		if (relativePath.isAbsolute())
 			relativePath.makeRelative(gProjectLibrary().getResourcesFolder());
 
-		MonoString* pathStr = MonoUtil::wstringToMono(MonoManager::instance().getDomain(), relativePath.toWString());
+		MonoString* pathStr = MonoUtil::wstringToMono(relativePath.toWString());
 		MonoUtil::invokeThunk(OnEntryImportedThunk, pathStr);
 	}
 
@@ -320,7 +320,7 @@ namespace BansheeEngine
 		Path relativePath = entry->path;
 		relativePath.makeRelative(gProjectLibrary().getResourcesFolder());
 
-		return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), relativePath.toWString());
+		return MonoUtil::wstringToMono(relativePath.toWString());
 	}
 
 	MonoString* ScriptLibraryEntry::internal_GetName(ScriptLibraryEntryBase* thisPtr)
@@ -329,7 +329,7 @@ namespace BansheeEngine
 		if (entry == nullptr)
 			return nullptr;
 
-		return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), entry->elementName);
+		return MonoUtil::wstringToMono(entry->elementName);
 	}
 
 	ProjectLibrary::LibraryEntryType ScriptLibraryEntry::internal_GetType(ScriptLibraryEntryBase* thisPtr)
@@ -439,7 +439,7 @@ namespace BansheeEngine
 		ProjectLibrary::ResourceEntry* fileEntry = static_cast<ProjectLibrary::ResourceEntry*>(entry);
 
 		if (fileEntry->meta != nullptr)
-			return MonoUtil::stringToMono(MonoManager::instance().getDomain(), fileEntry->meta->getUUID());
+			return MonoUtil::stringToMono(fileEntry->meta->getUUID());
 		else
 			return nullptr;
 	}

+ 2 - 2
SBansheeEditor/Source/BsScriptProjectSettings.cpp

@@ -34,7 +34,7 @@ namespace BansheeEngine
 	MonoString* ScriptProjectSettings::internal_GetLastOpenScene()
 	{
 		ProjectSettingsPtr settings = gEditorApplication().getProjectSettings();
-		return MonoUtil::stringToMono(MonoManager::instance().getDomain(), settings->getLastOpenScene());
+		return MonoUtil::stringToMono(settings->getLastOpenScene());
 	}
 
 	void ScriptProjectSettings::internal_SetLastOpenScene(MonoString* value)
@@ -108,7 +108,7 @@ namespace BansheeEngine
 		ProjectSettingsPtr settings = gEditorApplication().getProjectSettings();
 		WString nativeValue = settings->getString(nativeName, nativeDefaultValue);
 
-		return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), nativeValue);
+		return MonoUtil::wstringToMono(nativeValue);
 	}
 
 	bool ScriptProjectSettings::internal_HasKey(MonoString* name)

+ 3 - 3
SBansheeEditor/Source/BsScriptSelection.cpp

@@ -85,7 +85,7 @@ namespace BansheeEngine
 
 		for (UINT32 i = 0; i < (UINT32)uuids.size(); i++)
 		{
-			MonoString* monoString = MonoUtil::stringToMono(MonoManager::instance().getDomain(), uuids[i]);
+			MonoString* monoString = MonoUtil::stringToMono(uuids[i]);
 
 			void* elemAddr = mono_array_addr_with_size(uuidArray, sizeof(MonoString*), i);
 			memcpy(elemAddr, &monoString, sizeof(MonoString*));
@@ -119,7 +119,7 @@ namespace BansheeEngine
 
 		for (UINT32 i = 0; i < (UINT32)paths.size(); i++)
 		{
-			MonoString* monoString = MonoUtil::stringToMono(MonoManager::instance().getDomain(), paths[i].toString());
+			MonoString* monoString = MonoUtil::stringToMono(paths[i].toString());
 
 			void* elemAddr = mono_array_addr_with_size(pathArray, sizeof(MonoString*), i);
 			memcpy(elemAddr, &monoString, sizeof(MonoString*));
@@ -195,7 +195,7 @@ namespace BansheeEngine
 
 	void ScriptSelection::onResourcePing(const Path& resPath)
 	{
-		MonoString* monoResPath = MonoUtil::wstringToMono(MonoManager::instance().getDomain(), resPath.toWString());
+		MonoString* monoResPath = MonoUtil::wstringToMono(resPath.toWString());
 
 		MonoUtil::invokeThunk(OnPingResourceThunk, monoResPath);
 	}

+ 2 - 0
SBansheeEngine/Include/BsScriptEnginePrerequisites.h

@@ -76,6 +76,8 @@ namespace BansheeEngine
 	class ScriptHString;
 	class ScriptContextMenu;
 	class ScriptGUISkin;
+	class ScriptResourceRef;
+	class ScriptResourceRefBase;
 
 	typedef GameObjectHandle<ManagedComponent> HManagedComponent;
 	typedef ResourceHandle<ManagedResource> HManagedResource;

+ 57 - 10
SBansheeEngine/Include/BsScriptResourceRef.h

@@ -2,9 +2,39 @@
 
 #include "BsScriptEnginePrerequisites.h"
 #include "BsScriptObject.h"
+#include "BsTexture.h"
+#include "BsScriptResource.h"
+#include "BsMonoClass.h"
+#include "BsRTTIType.h"
 
 namespace BansheeEngine
 {
+	/**
+	 * @brief	Interop class between C++ & CLR for ResourceRefBase.
+	 */
+	class BS_SCR_BE_EXPORT ScriptResourceRefBase : public ScriptObject<ScriptResourceRefBase>
+	{
+	public:
+		SCRIPT_OBJ(ENGINE_ASSEMBLY, "BansheeEngine", "ResourceRefBase")
+
+		ScriptResourceRefBase(MonoObject* instance, const WeakResourceHandle<Resource>& resource);
+
+		/**
+		 * @brief	Returns a weak handle to the resource referenced by this object.
+		 */
+		WeakResourceHandle<Resource> getHandle() const { return mResource; }
+
+	private:
+		WeakResourceHandle<Resource> mResource;
+
+		/************************************************************************/
+		/* 								CLR HOOKS						   		*/
+		/************************************************************************/
+		static bool internal_IsLoaded(ScriptResourceRefBase* nativeInstance);
+		static MonoObject* internal_GetResource(ScriptResourceRefBase* nativeInstance);
+		static MonoString* internal_GetUUID(ScriptResourceRefBase* thisPtr);
+	};
+
 	/**
 	 * @brief	Interop class between C++ & CLR for ResourceRef.
 	 */
@@ -14,21 +44,38 @@ namespace BansheeEngine
 		SCRIPT_OBJ(ENGINE_ASSEMBLY, "BansheeEngine", "ResourceRef`1")
 
 		/**
-		 * @brief	Creates a new managed ResourceRef for the provided resource type.
+		 * @brief	Creates a new managed ResourceRef for the provided resource.
+		 *
+		 * @param	handle	Handle to the resource to wrap.
+		 */
+		template<class T>
+		static MonoObject* create(const WeakResourceHandle<T>& handle)
+		{
+			MonoClass* resourceClass = ScriptResource::getClassFromTypeId(T::getRTTIStatic()->getRTTIId());
+			if (resourceClass == nullptr)
+				return nullptr;
+
+			return create(resourceClass->_getInternalClass(), handle);
+		}
+
+		/**
+		 * @brief	Creates a new managed ResourceRef for the provided texture.
 		 *
-		 * @param	typeId	RTTI type ID of the resource to reference.
+		 * @param	handle	Handle to the texture to wrap.
 		 */
-		static MonoObject* create(UINT32 typeId);
+		static MonoObject* create(const WeakResourceHandle<Texture>& handle, TextureType type = TEX_TYPE_2D);
 
 	private:
-		ScriptResourceRef(MonoObject* instance, const WeakResourceHandle<Resource>& resource);
+		friend class ScriptResourceRefBase;
 
-		WeakResourceHandle<Resource> mResource;
+		ScriptResourceRef(MonoObject* instance);
 
-		/************************************************************************/
-		/* 								CLR HOOKS						   		*/
-		/************************************************************************/
-		static bool internal_IsLoaded(ScriptResourceRef* nativeInstance);
-		static MonoObject* internal_GetResource(ScriptResourceRef* nativeInstance);
+		/**
+		 * @brief	Creates a new managed ResourceRef for the provided resource type.
+		 *
+		 * @param	resourceClass	Managed class of the resource to reference.
+		 * @param	handle			Handle to the resource to wrap.
+		 */
+		static MonoObject* create(::MonoClass* resourceClass, const WeakResourceHandle<Resource>& handle);
 	};
 }

+ 1 - 0
SBansheeEngine/Include/BsScriptResources.h

@@ -20,6 +20,7 @@ namespace BansheeEngine
 		/* 								CLR HOOKS						   		*/
 		/************************************************************************/
 		static MonoObject* internal_Load(MonoString* path);
+		static MonoObject* internal_LoadRef(MonoObject* reference);
 		static void internal_UnloadUnused();
 	};
 }

+ 1 - 1
SBansheeEngine/Source/BsManagedSerializableField.cpp

@@ -600,7 +600,7 @@ namespace BansheeEngine
 			if(primitiveTypeInfo->mType == ScriptPrimitiveType::String)
 			{
 				if (!isNull)
-					return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), value);
+					return MonoUtil::wstringToMono(value);
 				else
 					return nullptr;
 			}

+ 2 - 2
SBansheeEngine/Source/BsScriptDebug.cpp

@@ -48,7 +48,7 @@ namespace BansheeEngine
 
 	void ScriptDebug::onLogEntryAdded(const LogEntry& entry)
 	{
-		MonoString* message = MonoUtil::stringToMono(MonoManager::instance().getDomain(), entry.getMessage());
+		MonoString* message = MonoUtil::stringToMono(entry.getMessage());
 
 		MonoUtil::invokeThunk(onAddedThunk, entry.getChannel(), message);
 	}
@@ -81,7 +81,7 @@ namespace BansheeEngine
 		ScriptArray output = ScriptArray::create<ScriptLogEntry>(numEntries);
 		for (UINT32 i = 0; i < numEntries; i++)
 		{
-			MonoString* message = MonoUtil::stringToMono(MonoManager::instance().getDomain(), entries[i].getMessage());
+			MonoString* message = MonoUtil::stringToMono(entries[i].getMessage());
 
 			ScriptLogEntryData scriptEntry = { entries[i].getChannel(), message };
 			output.set(i, scriptEntry);

+ 2 - 2
SBansheeEngine/Source/BsScriptGUIElement.cpp

@@ -284,11 +284,11 @@ namespace BansheeEngine
 			if (guiElemBase->_getType() == GUIElementBase::Type::Element)
 			{
 				GUIElement* guiElem = static_cast<GUIElement*>(guiElemBase);
-				return MonoUtil::stringToMono(MonoManager::instance().getDomain(), guiElem->getStyleName());
+				return MonoUtil::stringToMono(guiElem->getStyleName());
 			}
 		}
 
-		return MonoUtil::stringToMono(MonoManager::instance().getDomain(), StringUtil::BLANK);
+		return MonoUtil::stringToMono(StringUtil::BLANK);
 	}
 
 	void ScriptGUIElement::internal_SetStyle(ScriptGUIElementBaseTBase* nativeInstance, MonoString* style)

+ 2 - 2
SBansheeEngine/Source/BsScriptGUIInputBox.cpp

@@ -48,7 +48,7 @@ namespace BansheeEngine
 	void ScriptGUIInputBox::internal_getText(ScriptGUIInputBox* nativeInstance, MonoString** text)
 	{
 		GUIInputBox* inputBox = (GUIInputBox*)nativeInstance->getGUIElement();
-		*text = MonoUtil::wstringToMono(MonoManager::instance().getDomain(), inputBox->getText());
+		*text = MonoUtil::wstringToMono(inputBox->getText());
 	}
 
 	void ScriptGUIInputBox::internal_setText(ScriptGUIInputBox* nativeInstance, MonoString* text)
@@ -65,7 +65,7 @@ namespace BansheeEngine
 
 	void ScriptGUIInputBox::onChanged(MonoObject* instance, const WString& newValue)
 	{
-		MonoString* monoValue = MonoUtil::wstringToMono(MonoManager::instance().getDomain(), newValue);
+		MonoString* monoValue = MonoUtil::wstringToMono(newValue);
 		MonoUtil::invokeThunk(onChangedThunk, instance, monoValue);
 	}
 

+ 1 - 1
SBansheeEngine/Source/BsScriptHString.cpp

@@ -32,6 +32,6 @@ namespace BansheeEngine
 
 	void ScriptHString::internal_getValue(ScriptHString* nativeInstance, MonoString** value)
 	{
-		*value = MonoUtil::wstringToMono(MonoManager::instance().getDomain(), nativeInstance->mString.getValue());
+		*value = MonoUtil::wstringToMono(nativeInstance->mString.getValue());
 	}
 }

+ 1 - 1
SBansheeEngine/Source/BsScriptPlainText.cpp

@@ -37,7 +37,7 @@ namespace BansheeEngine
 	{
 		HPlainText plainText = thisPtr->getHandle();
 
-		return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), plainText->getString());
+		return MonoUtil::wstringToMono(plainText->getString());
 	}
 
 	void ScriptPlainText::internal_setText(ScriptPlainText* thisPtr, MonoString* text)

+ 2 - 2
SBansheeEngine/Source/BsScriptResource.cpp

@@ -153,11 +153,11 @@ namespace BansheeEngine
 
 	MonoString* ScriptResource::internal_getName(ScriptResourceBase* nativeInstance)
 	{
-		return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), nativeInstance->getGenericHandle()->getName());
+		return MonoUtil::wstringToMono(nativeInstance->getGenericHandle()->getName());
 	}
 
 	MonoString* ScriptResource::internal_getUUID(ScriptResourceBase* nativeInstance)
 	{
-		return MonoUtil::stringToMono(MonoManager::instance().getDomain(), nativeInstance->getGenericHandle().getUUID());
+		return MonoUtil::stringToMono(nativeInstance->getGenericHandle().getUUID());
 	}
 }

+ 50 - 21
SBansheeEngine/Source/BsScriptResourceRef.cpp

@@ -6,51 +6,80 @@
 #include "BsMonoUtil.h"
 #include "BsResources.h"
 #include "BsScriptResource.h"
+#include "BsScriptTexture2D.h"
+#include "BsScriptTexture3D.h"
+#include "BsScriptTextureCube.h"
 #include "BsScriptResourceManager.h"
 
 namespace BansheeEngine
 {
-	ScriptResourceRef::ScriptResourceRef(MonoObject* instance, const WeakResourceHandle<Resource>& resource)
+	ScriptResourceRefBase::ScriptResourceRefBase(MonoObject* instance, const WeakResourceHandle<Resource>& resource)
 		:ScriptObject(instance), mResource(resource)
 	{ }
 
-	void ScriptResourceRef::initRuntimeData()
+	void ScriptResourceRefBase::initRuntimeData()
+	{
+		metaData.scriptClass->addInternalCall("Internal_IsLoaded", &ScriptResourceRefBase::internal_IsLoaded);
+		metaData.scriptClass->addInternalCall("Internal_GetResource", &ScriptResourceRefBase::internal_GetResource);
+		metaData.scriptClass->addInternalCall("Internal_GetUUID", &ScriptResourceRefBase::internal_GetUUID);
+	}
+
+	bool ScriptResourceRefBase::internal_IsLoaded(ScriptResourceRefBase* nativeInstance)
 	{
-		metaData.scriptClass->addInternalCall("Internal_IsLoaded", &ScriptResourceRef::internal_IsLoaded);
-		metaData.scriptClass->addInternalCall("Internal_GetResource", &ScriptResourceRef::internal_GetResource);
+		return nativeInstance->mResource.isLoaded(false);
 	}
 
-	MonoObject* ScriptResourceRef::create(UINT32 typeId)
+	MonoObject* ScriptResourceRefBase::internal_GetResource(ScriptResourceRefBase* nativeInstance)
 	{
-		MonoClass* resourceClass = ScriptResource::getClassFromTypeId(typeId);
-		if (resourceClass == nullptr)
-			return nullptr;
+		HResource resource = gResources().load(nativeInstance->mResource);
+		ScriptResourceBase* scriptResource;
+
+		ScriptResourceManager::instance().getScriptResource(resource, &scriptResource, true);
+
+		return scriptResource->getManagedInstance();
+	}
+
+	MonoString* ScriptResourceRefBase::internal_GetUUID(ScriptResourceRefBase* thisPtr)
+	{
+		const String& uuid = thisPtr->getHandle().getUUID();
+
+		return MonoUtil::stringToMono(uuid);
+	}
+
+	ScriptResourceRef::ScriptResourceRef(MonoObject* instance)
+		:ScriptObject(instance)
+	{ }
+
+	void ScriptResourceRef::initRuntimeData()
+	{ }
 
-		MonoType* genParams[1] = { mono_class_get_type(resourceClass->_getInternalClass()) };
+	MonoObject* ScriptResourceRef::create(::MonoClass* resourceClass, const WeakResourceHandle<Resource>& handle)
+	{
+		MonoType* genParams[1] = { mono_class_get_type(resourceClass) };
 
 		::MonoClass* resourceRefClass = mono_class_bind_generic_parameters(metaData.scriptClass->_getInternalClass(), 1, genParams, false);
 		MonoObject* obj = mono_object_new(MonoManager::instance().getDomain(), resourceRefClass);
 		mono_runtime_object_init(obj);
 
-		WeakResourceHandle<Resource> emptyHandle;
-		ScriptResourceRef* scriptResourceRef = new (bs_alloc<ScriptResourceRef>()) ScriptResourceRef(obj, emptyHandle);
+		ScriptResourceRefBase* scriptResourceRef = new (bs_alloc<ScriptResourceRefBase>()) ScriptResourceRefBase(obj, handle);
 
 		return obj;
 	}
 
-	bool ScriptResourceRef::internal_IsLoaded(ScriptResourceRef* nativeInstance)
+	MonoObject* ScriptResourceRef::create(const WeakResourceHandle<Texture>& handle, TextureType type)
 	{
-		return nativeInstance->mResource.isLoaded(false);
-	}
+		switch (type)
+		{
+		case TEX_TYPE_2D:
+			return create(ScriptTexture2D::getMetaData()->scriptClass->_getInternalClass(), handle);
+		case TEX_TYPE_3D:
+			return create(ScriptTexture3D::getMetaData()->scriptClass->_getInternalClass(), handle);
+		case TEX_TYPE_CUBE_MAP:
+			return create(ScriptTextureCube::getMetaData()->scriptClass->_getInternalClass(), handle);
+		}
 
-	MonoObject* ScriptResourceRef::internal_GetResource(ScriptResourceRef* nativeInstance)
-	{
-		HResource resource = gResources().load(nativeInstance->mResource);
-		ScriptResourceBase* scriptResource;
+		return nullptr;
 
-		ScriptResourceManager::instance().getScriptResource(resource, &scriptResource, true);
-
-		return scriptResource->getManagedInstance();
 	}
 }
 

+ 10 - 0
SBansheeEngine/Source/BsScriptResources.cpp

@@ -7,6 +7,7 @@
 #include "BsGameResourceManager.h"
 #include "BsScriptResourceManager.h"
 #include "BsScriptResource.h"
+#include "BsScriptResourceRef.h"
 
 namespace BansheeEngine
 {
@@ -34,6 +35,15 @@ namespace BansheeEngine
 		return scriptResource->getManagedInstance();
 	}
 
+	MonoObject* ScriptResources::internal_LoadRef(MonoObject* reference)
+	{
+		ScriptResourceRef* scriptRef = ScriptResourceRef::toNative(reference);
+		if (scriptRef == nullptr)
+			return nullptr;
+
+		return scriptRef->getManagedInstance();
+	}
+
 	void ScriptResources::internal_UnloadUnused()
 	{
 		gResources().unloadAllUnused();

+ 1 - 1
SBansheeEngine/Source/BsScriptSceneObject.cpp

@@ -85,7 +85,7 @@ namespace BansheeEngine
 			return nullptr;
 
 		String name = nativeInstance->mSceneObject->getName();
-		return MonoUtil::stringToMono(MonoManager::instance().getDomain(), name);
+		return MonoUtil::stringToMono(name);
 	}
 
 	void ScriptSceneObject::internal_setActive(ScriptSceneObject* nativeInstance, bool value)

+ 2 - 2
SBansheeEngine/Source/BsScriptScriptCode.cpp

@@ -42,9 +42,9 @@ namespace BansheeEngine
 	{
 		HScriptCode scriptCode = thisPtr->getHandle();
 		if (!scriptCode.isLoaded())
-			MonoUtil::wstringToMono(MonoManager::instance().getDomain(), L"");
+			MonoUtil::wstringToMono(L"");
 
-		return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), scriptCode->getString());
+		return MonoUtil::wstringToMono(scriptCode->getString());
 	}
 
 	void ScriptScriptCode::internal_setText(ScriptScriptCode* thisPtr, MonoString* text)

+ 1 - 1
SBansheeEngine/Source/BsScriptSerializableField.cpp

@@ -24,7 +24,7 @@ namespace BansheeEngine
 
 	ScriptSerializableField* ScriptSerializableField::create(MonoObject* parentObject, const ManagedSerializableFieldInfoPtr& fieldInfo)
 	{
-		MonoString* monoStrName = MonoUtil::wstringToMono(MonoManager::instance().getDomain(), toWString(fieldInfo->mName));
+		MonoString* monoStrName = MonoUtil::wstringToMono(toWString(fieldInfo->mName));
 		MonoType* monoInternalType = mono_class_get_type(fieldInfo->mTypeInfo->getMonoClass());
 		MonoReflectionType* internalType = mono_type_get_object(MonoManager::instance().getDomain(), monoInternalType);
 		UINT32 fieldFlags = (UINT32)fieldInfo->mFlags;

+ 2 - 2
SBansheeEngine/Source/BsScriptStringTable.cpp

@@ -93,7 +93,7 @@ namespace BansheeEngine
 		WString nativeIdentifier = MonoUtil::monoToWString(identifier);
 		WString nativeValue = thisPtr->getHandle()->getString(nativeIdentifier, language);
 
-		*value = MonoUtil::wstringToMono(MonoManager::instance().getDomain(), nativeValue);
+		*value = MonoUtil::wstringToMono(nativeValue);
 	}
 
 	void ScriptStringTable::internal_GetStringDefault(ScriptStringTable* thisPtr, MonoString* identifier, MonoString** value)
@@ -101,7 +101,7 @@ namespace BansheeEngine
 		WString nativeIdentifier = MonoUtil::monoToWString(identifier);
 		WString nativeValue = thisPtr->getHandle()->getString(nativeIdentifier, StringTableManager::instance().getActiveLanguage());
 
-		*value = MonoUtil::wstringToMono(MonoManager::instance().getDomain(), nativeValue);
+		*value = MonoUtil::wstringToMono(nativeValue);
 	}
 
 	MonoObject* ScriptStringTable::createInstance()