Created
December 15, 2017 10:47
-
-
Save hiroki-o/45d965e66f8e8b8dd2975eafbaf8719c to your computer and use it in GitHub Desktop.
UnityChan Dynamic Setup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
using System; | |
using System.Collections.Generic; | |
using System.Text.RegularExpressions; | |
using UnityEngine.AssetGraph; | |
using UnityChan; | |
[System.Serializable] | |
public class SpringBoneReference : ObjectReferenceBase<SpringBone> { | |
public SpringBoneReference () : base() {} | |
public SpringBoneReference (SpringBoneReference r): base(r) {} | |
public SpringBoneReference (SpringBone obj) : base(obj) {} | |
} | |
[CustomModifier("AttachSpringBone", typeof(GameObject))] | |
public class AttachSpringBone : IModifier { | |
enum AttachPolicy { | |
RootObject = 1, | |
MiddleObject = 2, | |
LeafObject = 4 | |
} | |
[SerializeField] private AttachPolicy attachPolicy; | |
[SerializeField] private string nameFormat; | |
[SerializeField] private SpringBoneReference m_referenceComponent; | |
// Test if asset is different from intended configuration | |
public bool IsModified (UnityEngine.Object[] assets, List<AssetReference> group) { | |
return m_referenceComponent.Object != null; | |
} | |
// Actually change asset configurations. | |
public void Modify (UnityEngine.Object[] assets, List<AssetReference> group) { | |
// EditorUtility.CopySerializedIfDifferent | |
Regex r = new Regex(nameFormat); | |
bool isRootObjTargeting = (attachPolicy & AttachPolicy.RootObject) > 0; | |
bool isLeafObjTargeting = (attachPolicy & AttachPolicy.LeafObject) > 0; | |
bool isMiddleObjTargeting = (attachPolicy & AttachPolicy.MiddleObject) > 0; | |
foreach (var o in assets) { | |
GameObject go = o as GameObject; | |
if (go == null) { | |
continue; | |
} | |
if (!r.IsMatch (go.name)) { | |
continue; | |
} | |
bool isRootObj = go.transform.parent == null; | |
bool isLeafObj = go.transform.childCount == 0; | |
bool isMiddleObj = !isRootObj && !isLeafObj; | |
bool isTargeting = | |
(isRootObj && isRootObjTargeting) || | |
(isLeafObj && isLeafObjTargeting) || | |
(isMiddleObj && isMiddleObjTargeting); | |
if (!isTargeting) { | |
continue; | |
} | |
var springBone = go.GetComponent<SpringBone> (); | |
if(springBone == null) { | |
springBone = go.AddComponent<SpringBone> (); | |
} | |
EditorUtility.CopySerializedIfDifferent (m_referenceComponent.Object, springBone); | |
springBone.child = springBone.transform.GetChild (0); | |
} | |
} | |
// Draw inspector gui | |
public void OnInspectorGUI (Action onValueChanged) { | |
var newAttachPolicy = (AttachPolicy)EditorGUILayout.EnumFlagsField ("Attach Policy", attachPolicy); | |
if(newAttachPolicy != attachPolicy) { | |
attachPolicy = newAttachPolicy; | |
onValueChanged(); | |
} | |
var newNameFormat = EditorGUILayout.TextField ("Name Pattern", nameFormat); | |
if(newNameFormat != nameFormat) { | |
nameFormat = newNameFormat; | |
onValueChanged(); | |
} | |
var obj = (SpringBone)EditorGUILayout.ObjectField ("Reference Component", m_referenceComponent.Object, typeof(SpringBone)); | |
if(obj != m_referenceComponent.Object) { | |
m_referenceComponent.Object = obj; | |
onValueChanged(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
using System; | |
using System.Collections.Generic; | |
using System.Text.RegularExpressions; | |
using UnityEngine.AssetGraph; | |
using UnityChan; | |
[System.Serializable] | |
public class SpringManagerReference : ObjectReferenceBase<SpringManager> { | |
public SpringManagerReference() : base() {} | |
public SpringManagerReference(SpringManagerReference r): base(r) {} | |
public SpringManagerReference(SpringManager obj) : base(obj) {} | |
} | |
[CustomModifier("AttachSpringManager", typeof(GameObject))] | |
public class AttachSpringManager : IModifier { | |
enum AttachPolicy { | |
RootObject = 1, | |
MiddleObject = 2, | |
LeafObject = 4 | |
} | |
[SerializeField] private AttachPolicy attachPolicy; | |
[SerializeField] private string nameFormat; | |
[SerializeField] private SpringManagerReference m_referenceComponent; | |
// Test if asset is different from intended configuration | |
public bool IsModified (UnityEngine.Object[] assets, List<AssetReference> group) { | |
return true; | |
} | |
// Actually change asset configurations. | |
public void Modify (UnityEngine.Object[] assets, List<AssetReference> group) { | |
// EditorUtility.CopySerializedIfDifferent | |
Regex r = new Regex(nameFormat); | |
bool isRootObjTargeting = (attachPolicy & AttachPolicy.RootObject) > 0; | |
bool isLeafObjTargeting = (attachPolicy & AttachPolicy.LeafObject) > 0; | |
bool isMiddleObjTargeting = (attachPolicy & AttachPolicy.MiddleObject) > 0; | |
foreach (var o in assets) { | |
GameObject go = o as GameObject; | |
if (go == null) { | |
continue; | |
} | |
if (!r.IsMatch (go.name)) { | |
continue; | |
} | |
bool isRootObj = go.transform.parent == null; | |
bool isLeafObj = go.transform.childCount == 0; | |
bool isMiddleObj = !isRootObj && !isLeafObj; | |
bool isTargeting = | |
(isRootObj && isRootObjTargeting) || | |
(isLeafObj && isLeafObjTargeting) || | |
(isMiddleObj && isMiddleObjTargeting); | |
if (!isTargeting) { | |
continue; | |
} | |
var mgr = go.GetComponent<SpringManager> (); | |
if (mgr == null) { | |
mgr = go.AddComponent<SpringManager> (); | |
} | |
EditorUtility.CopySerializedIfDifferent (m_referenceComponent.Object, mgr); | |
mgr.springBones = mgr.GetComponentsInChildren<SpringBone> (); | |
} | |
} | |
// Draw inspector gui | |
public void OnInspectorGUI (Action onValueChanged) { | |
var newAttachPolicy = (AttachPolicy)EditorGUILayout.EnumFlagsField ("Attach Policy", attachPolicy); | |
if(newAttachPolicy != attachPolicy) { | |
attachPolicy = newAttachPolicy; | |
onValueChanged(); | |
} | |
var newNameFormat = EditorGUILayout.TextField ("Name Pattern", nameFormat); | |
if(newNameFormat != nameFormat) { | |
nameFormat = newNameFormat; | |
onValueChanged(); | |
} | |
var obj = (SpringManager)EditorGUILayout.ObjectField ("Reference Component", m_referenceComponent.Object, typeof(SpringManager)); | |
if(obj != m_referenceComponent.Object) { | |
m_referenceComponent.Object = obj; | |
onValueChanged(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment