Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UMA;
- using System;
- namespace Opsive.ThirdPersonController.Sample.UMA
- {
- /// <summary>
- /// A sample class which will add two abilities, the camera, and the pistol with bullets after the UMA character has been created.
- /// </summary>
- public class CharacterSetup : MonoBehaviour
- {
- [Tooltip("A reference to the pistol ItemType")]
- [SerializeField] protected ItemType m_PistolItemType;
- [Tooltip("A reference to the pistol bullets ItemType")]
- [SerializeField] protected ItemType m_PistolBulletItemType;
- [Tooltip("The number of bullets to pickup")]
- [SerializeField] protected int m_BulletCount = 10;
- /// <summary>
- /// Add two abilities and attach the camera to the character.
- /// </summary>
- /// <param name="umaData">The UMAData of the character just created.</param>
- public void SetupCharacter(UMAData umaData)
- {
- // Add the Speed Change ability.
- var controller = umaData.GetComponent<RigidbodyCharacterController>();
- AddAbility(controller, typeof(Abilities.Fall), "", Abilities.Ability.AbilityStartType.Automatic, Abilities.Ability.AbilityStopType.Manual);
- AddAbility(controller, typeof(Abilities.Jump), "Jump", Abilities.Ability.AbilityStartType.ButtonDown, Abilities.Ability.AbilityStopType.Automatic);
- // Set the camera to the character.
- /*var cameraController = Utility.FindCamera().GetComponent<CameraController>();
- if (cameraController != null) {
- cameraController.Character = umaData.gameObject;
- }*/
- // Add the pistol and the bullets to the default loadout.
- var inventory = umaData.GetComponent<Inventory>();
- var defaultLoadout = inventory.DefaultLoadout;
- if (defaultLoadout == null) {
- defaultLoadout = new Inventory.ItemAmount[0];
- }
- var newLoadout = new Inventory.ItemAmount[defaultLoadout.Length + 2];
- defaultLoadout.CopyTo(newLoadout, 0);
- newLoadout[newLoadout.Length - 2] = new Inventory.ItemAmount(m_PistolItemType, 1);
- newLoadout[newLoadout.Length - 1] = new Inventory.ItemAmount(m_PistolBulletItemType, 1);
- inventory.DefaultLoadout = newLoadout;
- }
- /// <summary>
- /// Adds the ability to the RigidbodyCharacterController.
- /// </summary>
- /// <param name="controller">A reference to the RigidbodyCharacterController.</param>
- /// <param name="type">The type of ability to add.</param>
- /// <param name="inputName">The ability input name. Can be empty.</param>
- /// <param name="startType">The ability StartType.</param>
- /// <param name="stopType">The ability StopType.</param>
- private void AddAbility(RigidbodyCharacterController controller, Type type, string inputName, Abilities.Ability.AbilityStartType startType, Abilities.Ability.AbilityStopType stopType)
- {
- var ability = controller.gameObject.AddComponent(type) as Abilities.Ability;
- // The RigidbodyCharacterController will show the ability inspector.
- ability.hideFlags = HideFlags.HideInInspector;
- // Set the base class values.
- ability.StartType = startType;
- ability.StopType = stopType;
- ability.InputName = inputName;
- // Add the ability to the RigidbodyCharacterController.
- var abilities = controller.Abilities;
- ability.Index = abilities.Length;
- Array.Resize(ref abilities, abilities.Length + 1);
- abilities[abilities.Length - 1] = ability;
- controller.Abilities = abilities;
- }
- /// <summary>
- /// The UMA character has changed. Reinitialize the components so they have the correct references.
- /// </summary>
- /// <param name="umaData">The UMAData of the character just updated.</param>
- public void UpdateCharacterSetup(UMAData umaData)
- {
- // Remove the Fall and Jump abilities.
- DestroyImmediate(umaData.GetComponent<Abilities.Fall>(), true);
- DestroyImmediate(umaData.GetComponent<Abilities.Jump>(), true);
- var controller = umaData.GetComponent<RigidbodyCharacterController>();
- controller.Abilities = new Abilities.Ability[0];
- // Add the ability again.
- AddAbility(controller, typeof(Abilities.Fall), "", Abilities.Ability.AbilityStartType.Automatic, Abilities.Ability.AbilityStopType.Manual);
- AddAbility(controller, typeof(Abilities.Jump), "Jump", Abilities.Ability.AbilityStartType.ButtonDown, Abilities.Ability.AbilityStopType.Automatic);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement