Skip to content

Instantly share code, notes, and snippets.

@frarees
Last active August 17, 2022 10:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frarees/8f4d7c5bb961e42b5dd471ab2b22b8ac to your computer and use it in GitHub Desktop.
Save frarees/8f4d7c5bb961e42b5dd471ab2b22b8ac to your computer and use it in GitHub Desktop.
C# Solution Sync for Unity3D
// https://frarees.github.io/default-gist-license
using UnityEngine;
using UnityEditor;
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine.Profiling;
[InitializeOnLoad]
static class SyncUtil
{
[InitializeOnLoad]
static class SyncVS
{
public static bool isValid { get; private set; }
static Type type;
static MethodInfo createIfDoesntExist;
static MethodInfo syncIfFirstFileOpenSinceDomainLoad;
static bool GetMembers()
{
type = typeof(UnityEditor.Editor).
Assembly.
GetType("UnityEditor.SyncVS");
Debug.Assert(type != null);
if (type == null)
return false;
createIfDoesntExist = type.
GetMethod("CreateIfDoesntExist",
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static,
null, CallingConventions.Any, new Type[] {}, null);
Debug.Assert(createIfDoesntExist != null);
if (createIfDoesntExist == null)
return false;
syncIfFirstFileOpenSinceDomainLoad = type.
GetMethod("SyncIfFirstFileOpenSinceDomainLoad",
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static,
null, CallingConventions.Any, new Type[] {}, null);
Debug.Assert(syncIfFirstFileOpenSinceDomainLoad != null);
if (syncIfFirstFileOpenSinceDomainLoad == null)
return false;
return true;
}
static SyncVS()
{
isValid = GetMembers();
}
public static void CreateIfDoesntExist()
{
createIfDoesntExist.Invoke(null, new object[] {});
}
public static void SyncIfFirstFileOpenSinceDomainLoad()
{
syncIfFirstFileOpenSinceDomainLoad.Invoke(null, new object[] {});
}
}
static bool reloading;
const string kAutoSync = "kAutoSync";
static bool autoSync
{
get
{
return EditorPrefs.GetBool(kAutoSync);
}
set
{
EditorPrefs.SetBool(kAutoSync, value);
}
}
static SyncUtil()
{
AssemblyReloadEvents.afterAssemblyReload += OnAfterAssemblyReload;
AssemblyReloadEvents.beforeAssemblyReload += OnBeforeAssemblyReload;
}
static void OnBeforeAssemblyReload()
{
reloading = true;
}
static void OnAfterAssemblyReload()
{
reloading = false;
if (autoSync)
Sync();
}
[SettingsProvider]
static SettingsProvider GetSettingsProvider()
{
return new SettingsProvider("Preferences/Sync", SettingsScope.User)
{
label = "Sync",
guiHandler = GUIHandler,
keywords = new HashSet<string>(new[] { "Sync" })
};
}
static void GUIHandler(string searchContext)
{
if (!SyncVS.isValid)
{
EditorGUILayout.HelpBox("Couldn't reflect SyncVS members.", MessageType.Error);
}
EditorGUI.BeginDisabledGroup(!SyncVS.isValid);
EditorGUI.BeginChangeCheck();
var v = EditorGUILayout.Toggle("Auto-Sync", autoSync);
if (EditorGUI.EndChangeCheck())
{
autoSync = v;
Event.current.Use();
}
EditorGUI.BeginDisabledGroup(reloading || EditorApplication.isCompiling || EditorApplication.isUpdating);
if (GUILayout.Button("Sync solution", GUILayout.Width(120)))
{
Sync();
}
EditorGUI.EndDisabledGroup();
EditorGUI.EndDisabledGroup();
}
static void Sync()
{
Profiler.BeginSample("Sync Solution");
SyncVS.CreateIfDoesntExist();
SyncVS.SyncIfFirstFileOpenSinceDomainLoad();
Profiler.EndSample();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment