Skip to content

Instantly share code, notes, and snippets.

@EndingCredits
Created May 26, 2018 13:04
Show Gist options
  • Save EndingCredits/35fdd1c96d87598336de7b8aef642a10 to your computer and use it in GitHub Desktop.
Save EndingCredits/35fdd1c96d87598336de7b8aef642a10 to your computer and use it in GitHub Desktop.
Script to copy dynamic bones to multiple objects
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