zero3growlithe

Unity 3D Script - Soul Switcher

Oct 3rd, 2014
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.40 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. // This script allows for example the player to switch "souls" meaning MonoBehaviours with other object/player
  6. // so that the target becomes the player and player becomes the target.
  7. // To use simply feed the script with an target gameObject in the inspector and press the "E" key to switch.
  8. // You can modify the script so it reacts to other inputs rather than fixed key press.
  9.  
  10. // Created by Damian Kościelny (zero3growlithe)
  11. // You are allowed to use this script in your commercial projects without necessarly putting me into credits
  12. // but would be great if you did ;)
  13. public class SoulSwitcher : MonoBehaviour
  14. {
  15.     public GameObject target;
  16.  
  17.     void Update (){
  18.         if(Input.GetKeyDown(KeyCode.E)){
  19.             if (target == null || target == gameObject)
  20.                 return;
  21.             SwitchSouls(gameObject, target);
  22.         }
  23.     }
  24.  
  25.     public List<string> excludeComponentsMask = new List<string>(){
  26.         "CameraImageEffects" // or something
  27.     };
  28.     void SwitchSouls (GameObject source, GameObject target){
  29.         // get components and their types from target and source before switching
  30.         Component[] targetComponents = target.GetComponents<MonoBehaviour>();
  31.         Component[] sourceComponents = source.GetComponents<MonoBehaviour>();
  32.         string[] targetComponentTypes = GetComponentsTypes(targetComponents);
  33.         string[] sourceComponentTypes = GetComponentsTypes(sourceComponents);
  34.        
  35.         // create a temporary doll to keep target components
  36.         GameObject doll = new GameObject(target.transform.name+"_doll");
  37.  
  38.         // create a copy of the target's components
  39.         int i;
  40.         for (i = 0; i < targetComponents.Length; i++){
  41.             print (sourceComponentTypes[i]);
  42.             if (!excludeComponentsMask.Contains(targetComponentTypes[i])){
  43.                 Component prevComponent = targetComponents[i];
  44.                 targetComponents[i] = CopyComponent(targetComponents[i], doll);
  45.                 Destroy(prevComponent);
  46.             }
  47.         }
  48.         doll.tag = target.tag;
  49.         // transfer the components from source to target
  50.         for (i = 0; i < sourceComponents.Length; i++){
  51.             if (!excludeComponentsMask.Contains(sourceComponentTypes[i])){
  52.                 CopyComponent(sourceComponents[i], target);
  53.                 Destroy(sourceComponents[i]);
  54.             }
  55.         }
  56.         target.tag = source.tag;
  57.         // copy the target components copy to the source
  58.         for (i = 0; i < targetComponents.Length; i++){
  59.             if (!excludeComponentsMask.Contains(targetComponentTypes[i])){
  60.                 CopyComponent(targetComponents[i], source);
  61.                 Destroy(targetComponents[i]);
  62.             }
  63.         }
  64.         source.tag = target.tag;
  65.         Destroy(doll);
  66.     }
  67.  
  68.     string[] GetComponentsTypes (Component[] sourceList){
  69.         string[] types = new string[sourceList.Length];
  70.         for (int c = 0; c < sourceList.Length; c++){
  71.             types[c] = sourceList[c].GetType().ToString();
  72.         }
  73.         return types;
  74.     }
  75.  
  76.     Component CopyComponent(Component original, GameObject destination){
  77.         System.Type type = original.GetType();
  78.         Component copy = destination.AddComponent(type);
  79.  
  80.         System.Reflection.FieldInfo[] fields = type.GetFields(
  81.             System.Reflection.BindingFlags.Public |
  82.             System.Reflection.BindingFlags.Static |
  83.             System.Reflection.BindingFlags.NonPublic |
  84.             System.Reflection.BindingFlags.Instance |
  85.             System.Reflection.BindingFlags.FlattenHierarchy);
  86.  
  87.         foreach (System.Reflection.FieldInfo field in fields){
  88.             field.SetValue(copy, field.GetValue(original));
  89.         }
  90.  
  91.         ((MonoBehaviour)copy).enabled = ((MonoBehaviour)original).enabled;
  92.         return copy;
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment