Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- // This script allows for example the player to switch "souls" meaning MonoBehaviours with other object/player
- // so that the target becomes the player and player becomes the target.
- // To use simply feed the script with an target gameObject in the inspector and press the "E" key to switch.
- // You can modify the script so it reacts to other inputs rather than fixed key press.
- // Created by Damian Kościelny (zero3growlithe)
- // You are allowed to use this script in your commercial projects without necessarly putting me into credits
- // but would be great if you did ;)
- public class SoulSwitcher : MonoBehaviour
- {
- public GameObject target;
- void Update (){
- if(Input.GetKeyDown(KeyCode.E)){
- if (target == null || target == gameObject)
- return;
- SwitchSouls(gameObject, target);
- }
- }
- public List<string> excludeComponentsMask = new List<string>(){
- "CameraImageEffects" // or something
- };
- void SwitchSouls (GameObject source, GameObject target){
- // get components and their types from target and source before switching
- Component[] targetComponents = target.GetComponents<MonoBehaviour>();
- Component[] sourceComponents = source.GetComponents<MonoBehaviour>();
- string[] targetComponentTypes = GetComponentsTypes(targetComponents);
- string[] sourceComponentTypes = GetComponentsTypes(sourceComponents);
- // create a temporary doll to keep target components
- GameObject doll = new GameObject(target.transform.name+"_doll");
- // create a copy of the target's components
- int i;
- for (i = 0; i < targetComponents.Length; i++){
- print (sourceComponentTypes[i]);
- if (!excludeComponentsMask.Contains(targetComponentTypes[i])){
- Component prevComponent = targetComponents[i];
- targetComponents[i] = CopyComponent(targetComponents[i], doll);
- Destroy(prevComponent);
- }
- }
- doll.tag = target.tag;
- // transfer the components from source to target
- for (i = 0; i < sourceComponents.Length; i++){
- if (!excludeComponentsMask.Contains(sourceComponentTypes[i])){
- CopyComponent(sourceComponents[i], target);
- Destroy(sourceComponents[i]);
- }
- }
- target.tag = source.tag;
- // copy the target components copy to the source
- for (i = 0; i < targetComponents.Length; i++){
- if (!excludeComponentsMask.Contains(targetComponentTypes[i])){
- CopyComponent(targetComponents[i], source);
- Destroy(targetComponents[i]);
- }
- }
- source.tag = target.tag;
- Destroy(doll);
- }
- string[] GetComponentsTypes (Component[] sourceList){
- string[] types = new string[sourceList.Length];
- for (int c = 0; c < sourceList.Length; c++){
- types[c] = sourceList[c].GetType().ToString();
- }
- return types;
- }
- Component CopyComponent(Component original, GameObject destination){
- System.Type type = original.GetType();
- Component copy = destination.AddComponent(type);
- System.Reflection.FieldInfo[] fields = type.GetFields(
- System.Reflection.BindingFlags.Public |
- System.Reflection.BindingFlags.Static |
- System.Reflection.BindingFlags.NonPublic |
- System.Reflection.BindingFlags.Instance |
- System.Reflection.BindingFlags.FlattenHierarchy);
- foreach (System.Reflection.FieldInfo field in fields){
- field.SetValue(copy, field.GetValue(original));
- }
- ((MonoBehaviour)copy).enabled = ((MonoBehaviour)original).enabled;
- return copy;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment