From 68cb85897b579a40b1f2c102e8fc415891f0dd3b Mon Sep 17 00:00:00 2001 From: Paul Du Bois Date: Tue, 9 Jul 2019 11:05:32 -0700 Subject: [PATCH] Create TbtSettings to reference BrushManifest Prep for future work. Also allows the toolkit to work in play mode in the editor https://github.com/googlevr/tilt-brush-toolkit/issues/35 Change-Id: Id000b01589c82fbb9bb6fd2e23d193c2936ba94b --- .../TiltBrush/Assets/Brushes/AllBrushes.asset | 1 - UnitySDK/Assets/TiltBrush/Resources.meta | 8 ++++ .../Resources/TiltBrushToolkitSettings.asset | 14 ++++++ .../TiltBrushToolkitSettings.asset.meta | 8 ++++ .../Assets/TiltBrush/Scripts/BrushManifest.cs | 28 +----------- .../Scripts/Editor/ModelImportSettings.cs | 6 +-- .../Assets/TiltBrush/Scripts/TbtSettings.cs | 44 +++++++++++++++++++ .../TiltBrush/Scripts/TbtSettings.cs.meta | 11 +++++ 8 files changed, 89 insertions(+), 31 deletions(-) create mode 100644 UnitySDK/Assets/TiltBrush/Resources.meta create mode 100644 UnitySDK/Assets/TiltBrush/Resources/TiltBrushToolkitSettings.asset create mode 100644 UnitySDK/Assets/TiltBrush/Resources/TiltBrushToolkitSettings.asset.meta create mode 100644 UnitySDK/Assets/TiltBrush/Scripts/TbtSettings.cs create mode 100644 UnitySDK/Assets/TiltBrush/Scripts/TbtSettings.cs.meta diff --git a/UnitySDK/Assets/TiltBrush/Assets/Brushes/AllBrushes.asset b/UnitySDK/Assets/TiltBrush/Assets/Brushes/AllBrushes.asset index 42df9505..c2becbb1 100644 --- a/UnitySDK/Assets/TiltBrush/Assets/Brushes/AllBrushes.asset +++ b/UnitySDK/Assets/TiltBrush/Assets/Brushes/AllBrushes.asset @@ -11,7 +11,6 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 8a4e1c4479d3ef54a83ac7187217ec6a, type: 3} m_Name: AllBrushes m_EditorClassIdentifier: - m_DefaultManifest: {fileID: 0} m_Brushes: - {fileID: 11400000, guid: cadc04d18daae874aa63168911a19ba6, type: 2} - {fileID: 11400000, guid: 94e865ca3b92ee5568334254825adac1, type: 2} diff --git a/UnitySDK/Assets/TiltBrush/Resources.meta b/UnitySDK/Assets/TiltBrush/Resources.meta new file mode 100644 index 00000000..5e3756d6 --- /dev/null +++ b/UnitySDK/Assets/TiltBrush/Resources.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 9ed670421d3657f41a8fb119c0cba7e0 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/UnitySDK/Assets/TiltBrush/Resources/TiltBrushToolkitSettings.asset b/UnitySDK/Assets/TiltBrush/Resources/TiltBrushToolkitSettings.asset new file mode 100644 index 00000000..c4a01361 --- /dev/null +++ b/UnitySDK/Assets/TiltBrush/Resources/TiltBrushToolkitSettings.asset @@ -0,0 +1,14 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: bd66b8f767459f44e84273c6abbfc402, type: 3} + m_Name: TiltBrushToolkitSettings + m_EditorClassIdentifier: + m_BrushManifest: {fileID: 11400000, guid: 2b3e59298bb8cfc47bddd1e1ea01be06, type: 2} diff --git a/UnitySDK/Assets/TiltBrush/Resources/TiltBrushToolkitSettings.asset.meta b/UnitySDK/Assets/TiltBrush/Resources/TiltBrushToolkitSettings.asset.meta new file mode 100644 index 00000000..49a5e6d0 --- /dev/null +++ b/UnitySDK/Assets/TiltBrush/Resources/TiltBrushToolkitSettings.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 7389d4def70b23346b5dff39cddd398b +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/UnitySDK/Assets/TiltBrush/Scripts/BrushManifest.cs b/UnitySDK/Assets/TiltBrush/Scripts/BrushManifest.cs index 8a05dc86..929507ed 100644 --- a/UnitySDK/Assets/TiltBrush/Scripts/BrushManifest.cs +++ b/UnitySDK/Assets/TiltBrush/Scripts/BrushManifest.cs @@ -20,32 +20,6 @@ namespace TiltBrushToolkit { public class BrushManifest : ScriptableObject { - // static API - - private static BrushManifest sm_Instance; - public static BrushManifest Instance { - get { - // How can we get a reference to the singleton AllBrushes.asset? - // LoadAssetAtPath<>() is fragile, because it bakes in a file path. - // Instead, (ab)use the ability for scripts to have default values - // (which are stored in the .meta) - if (sm_Instance == null) { - var dummy = CreateInstance(); - sm_Instance = dummy.m_DefaultManifest; - DestroyImmediate(dummy); - } - Debug.Assert(sm_Instance != null, "Misconfigured singleton"); - return sm_Instance; - } - } - - // instance API - - // See Instance.get for an explanation of this - [HideInInspector] - [SerializeField] - private BrushManifest m_DefaultManifest; - [SerializeField] private BrushDescriptor[] m_Brushes; private Dictionary m_ByGuid; private ILookup m_ByName; @@ -76,7 +50,7 @@ public ILookup BrushesByName { #if UNITY_EDITOR [UnityEditor.MenuItem("Tilt Brush/Update Manifest")] public static void MenuItem_UpdateManifest() { - BrushManifest manifest = Instance; + BrushManifest manifest = TbtSettings.BrushManifest; manifest.m_Brushes = UnityEditor.AssetDatabase.FindAssets("t:BrushDescriptor") .Select(g => UnityEditor.AssetDatabase.GUIDToAssetPath(g)) .Select(p => UnityEditor.AssetDatabase.LoadAssetAtPath(p)) diff --git a/UnitySDK/Assets/TiltBrush/Scripts/Editor/ModelImportSettings.cs b/UnitySDK/Assets/TiltBrush/Scripts/Editor/ModelImportSettings.cs index feb7d942..76f3dcd4 100644 --- a/UnitySDK/Assets/TiltBrush/Scripts/Editor/ModelImportSettings.cs +++ b/UnitySDK/Assets/TiltBrush/Scripts/Editor/ModelImportSettings.cs @@ -321,7 +321,7 @@ void PerformTexcoordCoordinateConversion(BrushDescriptor desc, Mesh mesh, int uv void OnPostprocessModel(GameObject g) { // For backwards compatibility, if people have projects that use the old naming if (sm_forceOldMeshNamingConvention) { - Dictionary lookup = BrushManifest.Instance.AllBrushes + Dictionary lookup = TbtSettings.BrushManifest.AllBrushes .ToDictionary(desc => desc.m_Material); ChangeNamesRecursive(lookup, d => d.m_DurableName + "_geo", g.transform); } @@ -375,7 +375,7 @@ BrushDescriptor GetDescriptorForStroke(string oldMaterialName) { if (match.Success) { Guid brushGuid = new Guid(match.Groups[1].Value); try { - return BrushManifest.Instance.BrushesByGuid[brushGuid]; + return TbtSettings.BrushManifest.BrushesByGuid[brushGuid]; } catch (KeyNotFoundException) { LogWarningWithContext(string.Format( "Unexpected: Couldn't find Tilt Brush material for guid {0}.", @@ -386,7 +386,7 @@ BrushDescriptor GetDescriptorForStroke(string oldMaterialName) { // Older versions use material.name == brush.m_DurableName { - var descs = BrushManifest.Instance.BrushesByName[oldMaterialName].ToArray(); + var descs = TbtSettings.BrushManifest.BrushesByName[oldMaterialName].ToArray(); if (descs.Length == 1) { return descs[0]; } else if (descs.Length > 1) { diff --git a/UnitySDK/Assets/TiltBrush/Scripts/TbtSettings.cs b/UnitySDK/Assets/TiltBrush/Scripts/TbtSettings.cs new file mode 100644 index 00000000..174d3e7d --- /dev/null +++ b/UnitySDK/Assets/TiltBrush/Scripts/TbtSettings.cs @@ -0,0 +1,44 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using UnityEngine; + +namespace TiltBrushToolkit { + +public class TbtSettings : ScriptableObject { + const string kAssetName = "TiltBrushToolkitSettings"; + + private static TbtSettings sm_Instance; + + public static TbtSettings Instance { + get { + if (sm_Instance == null) { + sm_Instance = Resources.Load(kAssetName); + if (sm_Instance == null) { + throw new InvalidOperationException("Cannot find " + kAssetName + ".asset"); + } + } + return sm_Instance; + } + } + + public static BrushManifest BrushManifest { + get { return Instance.m_BrushManifest; } + } + + [SerializeField] private BrushManifest m_BrushManifest; +} + +} diff --git a/UnitySDK/Assets/TiltBrush/Scripts/TbtSettings.cs.meta b/UnitySDK/Assets/TiltBrush/Scripts/TbtSettings.cs.meta new file mode 100644 index 00000000..e7fcc2f1 --- /dev/null +++ b/UnitySDK/Assets/TiltBrush/Scripts/TbtSettings.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: bd66b8f767459f44e84273c6abbfc402 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: