Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// MIT License
//
// Copyright (c) 2019 Sabresaurus
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
using UnityEngine;
using UnityEditor;
namespace Sabresaurus.SabreCSG
{
/// <summary>
/// Version of PaletteWindow that is backed by a scriptable object instead of player prefs allowing palette tabs
/// to be shared through files (e.g. version control). Useful if multiple people want to work with the same set of
/// objects, e.g. multiple level designers using the same common prefabs
/// </summary>
public class SharedPaletteWindow : PaletteWindow
{
[MenuItem("Window/Shared Palette")]
static void CreateAndShow()
{
EditorWindow window = GetWindow<SharedPaletteWindow>("Shared Palette");
window.minSize = new Vector2(120, 120);
window.Show();
}
/// <summary>
/// Gets a reference to the shared palette data in assets, or if it can't find one - makes one and returns it
/// </summary>
SharedPaletteData GetOrCreateSharedPaletteData()
{
string[] matchedAssetGUIDs = AssetDatabase.FindAssets("t:SharedPaletteData");
if (matchedAssetGUIDs.Length >= 1)
{
return AssetDatabase.LoadAssetAtPath<SharedPaletteData>(AssetDatabase.GUIDToAssetPath(matchedAssetGUIDs[0]));
}
else
{
// Couldn't find one in Assets, so make a new one
string path = AssetDatabase.GenerateUniqueAssetPath("Assets/SharedPaletteData.asset");
SharedPaletteData newInstance = CreateInstance<SharedPaletteData>();
AssetDatabase.CreateAsset(newInstance, path);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
return newInstance;
}
}
protected override void LoadImplementation(int tabIndex, out string trackedString, out string tabName)
{
var sharedPaletteData = GetOrCreateSharedPaletteData();
if (sharedPaletteData != null && sharedPaletteData.TabDatas.Count > tabIndex)
{
trackedString = sharedPaletteData.TabDatas[tabIndex].TrackedString;
tabName = sharedPaletteData.TabDatas[tabIndex].TabName;
}
else
{
trackedString = "";
tabName = "";
}
}
protected override void SaveImplementation(int tabIndex, string tabName, string outputString)
{
var sharedPaletteData = GetOrCreateSharedPaletteData();
if (sharedPaletteData != null)
{
Undo.RecordObject(sharedPaletteData, "Change Palette");
// Make sure there's enough tab data's to accomodate the tab we're saving
while (tabIndex >= sharedPaletteData.TabDatas.Count)
{
sharedPaletteData.TabDatas.Add(new SharedPaletteData.TabData());
}
sharedPaletteData.TabDatas[tabIndex].TabName = tabName;
sharedPaletteData.TabDatas[tabIndex].TrackedString = outputString;
// Make sure Unity knows the asset has changed so it saves it to disk
EditorUtility.SetDirty(sharedPaletteData);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.