Skip to content

Commit

Permalink
🐛 fix(static-profile): Add watcher at once
Browse files Browse the repository at this point in the history
  • Loading branch information
esnya committed Jul 16, 2021
1 parent 81760be commit fd6b887
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions Assets/EsnyaUnityTools/Scripts/StaticProfile.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Text.RegularExpressions;
#if UNITY_EDITOR
using UnityEditor;
Expand All @@ -11,38 +11,37 @@

namespace EsnyaFactory
{
[
DisallowMultipleComponent,
ExecuteInEditMode,
]
[DisallowMultipleComponent]
public class StaticProfile : MonoBehaviour
{
#if UNITY_EDITOR
public StaticEditorFlags staticFlags = (StaticEditorFlags)0xFFFFFF;
[Multiline] public string staticExcludePattern = "^$";

public float lightmapScaleOffset = 1.0f;
public LightmapParameters lightmapParameters;
public bool convexMeshCollider;
[Multiline] public string colliderExcludePattern = "^$";

private void Start()
{
EditorSceneManager.sceneSaving += (_, __) => Apply();
}
public bool convexMeshCollider;
[Multiline] public string convexInvertPattern = "^$";

public void Apply()
{
gameObject.tag = "EditorOnly";

var staticExcludeRegex = new Regex(staticExcludePattern);
var origin = transform.parent ?? transform;
foreach (var o in origin.GetComponentsInChildren<Transform>().Select(t => t.gameObject)) GameObjectUtility.SetStaticEditorFlags(o, o == gameObject ? 0 : staticFlags);
foreach (var o in origin.GetComponentsInChildren<Transform>().Select(t => t.gameObject).Where(o => !staticExcludeRegex.IsMatch(o.name)))
{
GameObjectUtility.SetStaticEditorFlags(o, o == gameObject ? 0 : staticFlags);
}

var serializedObject = new SerializedObject(origin.GetComponentsInChildren<Renderer>());
serializedObject.FindProperty("m_ScaleInLightmap").floatValue = lightmapScaleOffset;
serializedObject.FindProperty("m_LightmapParameters").objectReferenceValue = lightmapParameters;
serializedObject.ApplyModifiedProperties();

var colliderExcludeRegex = new Regex(colliderExcludePattern);
foreach (var collider in origin.GetComponentsInChildren<MeshCollider>()) collider.convex = convexMeshCollider ^ colliderExcludeRegex.IsMatch(collider.gameObject.name);
var convexInvertRegex = new Regex(convexInvertPattern);
foreach (var collider in origin.GetComponentsInChildren<MeshCollider>()) collider.convex = convexMeshCollider ^ convexInvertRegex.IsMatch(collider.gameObject.name);
}
#endif
}
Expand Down Expand Up @@ -80,6 +79,21 @@ public override void OnInspectorGUI()
foreach (var p in targets.Select(t => t as StaticProfile).Where(p => p != null)) p.Apply();
}
}

[InitializeOnLoadMethod]
private static void RegisterCallback()
{
EditorSceneManager.sceneSaving += (_, __) => ApplyAll();
}

private static void ApplyAll()
{
foreach (var p in SceneManager.GetActiveScene().GetRootGameObjects().SelectMany(o => o.GetComponentsInChildren<StaticProfile>()))
{
p.Apply();
}
}
}
#endif

}

0 comments on commit fd6b887

Please sign in to comment.