Created
May 26, 2018 13:04
-
-
Save EndingCredits/35fdd1c96d87598336de7b8aef642a10 to your computer and use it in GitHub Desktop.
Script to copy dynamic bones to multiple objects
This file contains hidden or 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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
[ExecuteInEditMode] | |
public class DynBoneCopy : MonoBehaviour { | |
public DynamicBone template; | |
public bool Make_Permenant; | |
public bool confirm; | |
public List<Transform> objects = new List<Transform>(); | |
// Use this for initialization | |
void Start() { | |
if (Application.isPlaying) | |
{ | |
CopyComponents(); | |
} | |
} | |
void Update() | |
{ | |
if (Make_Permenant & confirm) | |
{ | |
Make_Permenant = false; | |
confirm = false; | |
CopyComponents(); | |
DestroyImmediate(this); | |
} | |
} | |
void CopyComponents() | |
{ | |
foreach (Transform obj in objects) | |
{ | |
DynamicBone template_copy = CopyComponent(template); | |
template_copy.m_Root = obj; | |
} | |
} | |
T CopyComponent<T>(T original) where T : Component | |
{ | |
System.Type type = original.GetType(); | |
Component copy = gameObject.AddComponent(type); | |
System.Reflection.FieldInfo[] fields = type.GetFields(); | |
foreach (System.Reflection.FieldInfo field in fields) | |
{ | |
field.SetValue(copy, field.GetValue(original)); | |
} | |
return copy as T; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment