Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created May 29, 2014 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/0568e1294b1c4dd23e6a to your computer and use it in GitHub Desktop.
Save tsubaki/0568e1294b1c4dd23e6a to your computer and use it in GitHub Desktop.
ネステッドプレハブ試作2号
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
}
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
}
@tsubaki
Copy link
Author

tsubaki commented May 29, 2014

使い方

  1. プレハブのネスト化する前に全プレハブにAttatchedPrefabを貼っておく
  2. プレハブをネスト化

効果

  • PIPがenableの場合、ネスト元のプレハブのコンポーネントはネストしたプレハブに反映
    ただしプレハブの変更は出来ない
  • PIPのApply Prefabでネステッドプレハブの情報をプレハブへ反映
  • PIPのReset Prefabでプレハブの情報をネステッドプレハブへ反映

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment