Advertisement
diegographics

Untitled

May 17th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.78 KB | None | 0 0
  1. using UnityEngine;
  2. using UMA;
  3. using System;
  4.  
  5. namespace Opsive.ThirdPersonController.Sample.UMA
  6. {
  7.     /// <summary>
  8.     /// A sample class which will add two abilities, the camera, and the pistol with bullets after the UMA character has been created.
  9.     /// </summary>
  10.     public class CharacterSetup : MonoBehaviour
  11.     {
  12.         [Tooltip("A reference to the pistol ItemType")]
  13.         [SerializeField] protected ItemType m_PistolItemType;
  14.         [Tooltip("A reference to the pistol bullets ItemType")]
  15.         [SerializeField] protected ItemType m_PistolBulletItemType;
  16.         [Tooltip("The number of bullets to pickup")]
  17.         [SerializeField] protected int m_BulletCount = 10;
  18.  
  19.         /// <summary>
  20.         /// Add two abilities and attach the camera to the character.
  21.         /// </summary>
  22.         /// <param name="umaData">The UMAData of the character just created.</param>
  23.         public void SetupCharacter(UMAData umaData)
  24.         {
  25.             // Add the Speed Change ability.
  26.             var controller = umaData.GetComponent<RigidbodyCharacterController>();
  27.             AddAbility(controller, typeof(Abilities.Fall), "", Abilities.Ability.AbilityStartType.Automatic, Abilities.Ability.AbilityStopType.Manual);
  28.             AddAbility(controller, typeof(Abilities.Jump), "Jump", Abilities.Ability.AbilityStartType.ButtonDown, Abilities.Ability.AbilityStopType.Automatic);
  29.  
  30.             // Set the camera to the character.
  31.             /*var cameraController = Utility.FindCamera().GetComponent<CameraController>();
  32.             if (cameraController != null) {
  33.                 cameraController.Character = umaData.gameObject;
  34.             }*/
  35.  
  36.             // Add the pistol and the bullets to the default loadout.
  37.             var inventory = umaData.GetComponent<Inventory>();
  38.             var defaultLoadout = inventory.DefaultLoadout;
  39.             if (defaultLoadout == null) {
  40.                 defaultLoadout = new Inventory.ItemAmount[0];
  41.             }
  42.             var newLoadout = new Inventory.ItemAmount[defaultLoadout.Length + 2];
  43.             defaultLoadout.CopyTo(newLoadout, 0);
  44.             newLoadout[newLoadout.Length - 2] = new Inventory.ItemAmount(m_PistolItemType, 1);
  45.             newLoadout[newLoadout.Length - 1] = new Inventory.ItemAmount(m_PistolBulletItemType, 1);
  46.             inventory.DefaultLoadout = newLoadout;
  47.         }
  48.  
  49.         /// <summary>
  50.         /// Adds the ability to the RigidbodyCharacterController.
  51.         /// </summary>
  52.         /// <param name="controller">A reference to the RigidbodyCharacterController.</param>
  53.         /// <param name="type">The type of ability to add.</param>
  54.         /// <param name="inputName">The ability input name. Can be empty.</param>
  55.         /// <param name="startType">The ability StartType.</param>
  56.         /// <param name="stopType">The ability StopType.</param>
  57.         private void AddAbility(RigidbodyCharacterController controller, Type type, string inputName, Abilities.Ability.AbilityStartType startType, Abilities.Ability.AbilityStopType stopType)
  58.         {
  59.             var ability = controller.gameObject.AddComponent(type) as Abilities.Ability;
  60.             // The RigidbodyCharacterController will show the ability inspector.
  61.             ability.hideFlags = HideFlags.HideInInspector;
  62.  
  63.             // Set the base class values.
  64.             ability.StartType = startType;
  65.             ability.StopType = stopType;
  66.             ability.InputName = inputName;
  67.  
  68.             // Add the ability to the RigidbodyCharacterController.
  69.             var abilities = controller.Abilities;
  70.             ability.Index = abilities.Length;
  71.             Array.Resize(ref abilities, abilities.Length + 1);
  72.             abilities[abilities.Length - 1] = ability;
  73.             controller.Abilities = abilities;
  74.         }
  75.  
  76.         /// <summary>
  77.         /// The UMA character has changed. Reinitialize the components so they have the correct references.
  78.         /// </summary>
  79.         /// <param name="umaData">The UMAData of the character just updated.</param>
  80.         public void UpdateCharacterSetup(UMAData umaData)
  81.         {
  82.             // Remove the Fall and Jump abilities.
  83.             DestroyImmediate(umaData.GetComponent<Abilities.Fall>(), true);
  84.             DestroyImmediate(umaData.GetComponent<Abilities.Jump>(), true);
  85.             var controller = umaData.GetComponent<RigidbodyCharacterController>();
  86.             controller.Abilities = new Abilities.Ability[0];
  87.  
  88.             // Add the ability again.
  89.             AddAbility(controller, typeof(Abilities.Fall), "", Abilities.Ability.AbilityStartType.Automatic, Abilities.Ability.AbilityStopType.Manual);
  90.             AddAbility(controller, typeof(Abilities.Jump), "Jump", Abilities.Ability.AbilityStartType.ButtonDown, Abilities.Ability.AbilityStopType.Automatic);
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement