Skip to content

Commit

Permalink
Create TbtSettings to reference BrushManifest
Browse files Browse the repository at this point in the history
Prep for future work.
Also allows the toolkit to work in play mode in the editor
googlevr#35

Change-Id: Id000b01589c82fbb9bb6fd2e23d193c2936ba94b
  • Loading branch information
dubois committed Jul 9, 2019
1 parent 8f16ee4 commit 68cb858
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 31 deletions.
1 change: 0 additions & 1 deletion UnitySDK/Assets/TiltBrush/Assets/Brushes/AllBrushes.asset
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
8 changes: 8 additions & 0 deletions UnitySDK/Assets/TiltBrush/Resources.meta

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

14 changes: 14 additions & 0 deletions UnitySDK/Assets/TiltBrush/Resources/TiltBrushToolkitSettings.asset
Original file line number Diff line number Diff line change
@@ -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}

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

28 changes: 1 addition & 27 deletions UnitySDK/Assets/TiltBrush/Scripts/BrushManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BrushManifest>();
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<Guid, BrushDescriptor> m_ByGuid;
private ILookup<string, BrushDescriptor> m_ByName;
Expand Down Expand Up @@ -76,7 +50,7 @@ public ILookup<string, BrushDescriptor> 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<BrushDescriptor>(p))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Material, BrushDescriptor> lookup = BrushManifest.Instance.AllBrushes
Dictionary<Material, BrushDescriptor> lookup = TbtSettings.BrushManifest.AllBrushes
.ToDictionary(desc => desc.m_Material);
ChangeNamesRecursive(lookup, d => d.m_DurableName + "_geo", g.transform);
}
Expand Down Expand Up @@ -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}.",
Expand All @@ -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) {
Expand Down
44 changes: 44 additions & 0 deletions UnitySDK/Assets/TiltBrush/Scripts/TbtSettings.cs
Original file line number Diff line number Diff line change
@@ -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<TbtSettings>(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;
}

}
11 changes: 11 additions & 0 deletions UnitySDK/Assets/TiltBrush/Scripts/TbtSettings.cs.meta

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

0 comments on commit 68cb858

Please sign in to comment.