Created
May 29, 2014 18:05
-
-
Save tsubaki/0568e1294b1c4dd23e6a to your computer and use it in GitHub Desktop.
ネステッドプレハブ試作2号
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 System.Collections; | |
[ExecuteInEditMode] | |
public class AttatchedPrefab : MonoBehaviour { | |
#if UNITY_EDITOR | |
void Start () { | |
var pip = gameObject.AddComponent<PIP>(); | |
pip.basePrefab =(GameObject) UnityEditor.PrefabUtility.GetPrefabParent(gameObject); | |
DestroyImmediate (this); | |
} | |
#endif | |
} |
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 System.Collections; | |
using System.Collections.Generic; | |
[ExecuteInEditMode] | |
public class PIP : MonoBehaviour | |
{ | |
#if UNITY_EDITOR | |
[HideInInspector] | |
public GameObject basePrefab; | |
void Update() | |
{ | |
UpdateComponents(); | |
} | |
[ContextMenu("Reset Prefab")] | |
void UpdateComponents() | |
{ | |
UpdateComponent(basePrefab, gameObject); | |
} | |
[ContextMenu("Apply Prefab")] | |
void ApplyComponents() | |
{ | |
UpdateComponent(gameObject, basePrefab); | |
} | |
void UpdateComponent(GameObject baseObject, GameObject postObject) | |
{ | |
var baseComponents = new List<Component>(baseObject.GetComponents(typeof(Component))); | |
var postComponents = new List<Component>( postObject.GetComponents(typeof(Component)) ); | |
foreach( var component in baseComponents) | |
{ | |
if( component is PIP || component is AttatchedPrefab ) | |
{ | |
continue; | |
} | |
var targetComponent = postComponents.Find( (item )=> item.GetType() == component.GetType ()); | |
UnityEditorInternal.ComponentUtility.CopyComponent(component); | |
if( targetComponent != null ){ | |
UnityEditorInternal.ComponentUtility.PasteComponentValues(targetComponent); | |
}else{ | |
UnityEditorInternal.ComponentUtility.PasteComponentAsNew(gameObject); | |
} | |
} | |
foreach( var component in postComponents ) | |
{ | |
if( component is PIP || component is AttatchedPrefab ) | |
{ | |
continue; | |
} | |
var targetComponent = baseComponents.Find( (item )=> item.GetType() == component.GetType ()); | |
if( targetComponent == null ) | |
{ | |
DestroyImmediate (component); | |
} | |
} | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
使い方
効果
ただしプレハブの変更は出来ない