我已经使用youtube示例实现了一个帮助脚本。但是当我在其他脚本中使用 Resource 方法时,编译器会给出一个空引用错误。我究竟做错了什么。
这是我的帮助脚本以及如何调用资源。
using UnityEngine;
using System.Collections;
namespace Helper
{
public class Resource
{
public static string AnimatorController = "System/PlayerAnimator";
}
}
根据我的场景的路径是
资产/资源/系统/PlayerAnimator.controller
这就是我在其他脚本中用来加载资源的方式
private Animator _animator;
private RuntimeAnimatorController _animatorController;
_animatorController = Resources.Load(Resource.AnimatorController) as RuntimeAnimatorController;
_animator.runtimeAnimatorController = _animatorController;
解决方法是什么?
我添加了两个脚本。非没有工作
PlayerCharctor.cs
using UnityEngine; using System.Collections; using Helper;
[RequireComponent(typeof(NetworkView))] [RequireComponent(typeof(CharacterController))] [RequireComponent(typeof(Animator))]
[RequireComponent(typeof(PlayerMotor))] [RequireComponent(typeof(PlayerCamera))]
[AddComponentMenu("Scripts/Player/PlayerCharacter")] public class PlayerCharacter : MonoBehaviour {
#region Variables & Properties (Private)
private CharacterController _controller;
private Animator _animator;
private RuntimeAnimatorController _animatorController;
#endregion
#region Variables & Properties (Public)
public CharacterController Controller
{
get
{
return _controller;
}
}
public Animator Animator
{
get
{
return _animator;
}
}
#endregion
#region Unity Event Funtions
void awake()
{
_animator = GetComponent<Animator>();
_controller = GetComponent<CharacterController>();
}
// Use this for initialization
void Start ()
{
//Ensure networkview Component exists
if (GetComponent<NetworkView>() != null)
{
//Ensure that initialization only executes if this is a valid instance
if (GetComponent<NetworkView>().isMine || Network.peerType == NetworkPeerType.Disconnected)
{
//Load in the AnimatorController at runtime
_animatorController = Resources.Load(Resource.AnimatorController) as RuntimeAnimatorController;
_animator.runtimeAnimatorController = _animatorController;
_controller.center = new Vector3(0f, 1f, 0f);
_controller.height = 1.8f;
}
else
{
enabled = false;
}
}
else
{
Debug.Log("Attach a NetWorkViewComponent!!");
}
}
// Update is called once per frame
void Update ()
{
}
#endregion
#region Methods
#endregion Methods }
和助手脚本
using UnityEngine;
using System.Collections;
namespace Helper
{
#region Referance Cache
public class PlayerInput
{
public static string Horizontal = "Horizontal";
public static string Vertical = "vertical";
public static string Jump = "Jump";
public static string RightX = "Mouse X";
public static string RightY = "Mouse Y";
}
public class GameTag
{
//System tags
public static string Untagged = "Untagged";
public static string Respawn = "Respawn";
public static string Finish = "Finish";
public static string EditorOnly = "EditorOnly";
public static string MainCamera = "MainCamera";
public static string Player = "Player";
public static string GameController = "GameController";
public static string PlayerCamera = "PlayerCamera";
}
public class Resource
{
public static string AnimatorController= "System/PLController"; //Changed the name of the controller (new one)
}
public static class AnimatorConditions
{
public static string Speed = "Speed";
public static string Direction = "Direction";
public static string Grounded = "Grounded";
public static string AirVelocity = "AirVelocity";
}
#endregion
#region FSM Enumerations (Finite State Machine)
public enum CameraState
{
Normal,
Target
}
public enum SpeedState
{
Walk,
Run,
Sprint
}
#endregion
#region Object Structures
public struct CameraTargetObject
{
private Vector3 position;
private Transform xForm;
public Vector3 Position
{
get
{
return position;
}
set
{
position = value;
}
}
public Transform XForm
{
get
{
return xForm;
}
set
{
xForm = value;
}
}
public void Init(string camName, Vector3 pos, Transform transform, Transform parent)
{
position = pos;
xForm = transform;
xForm.name = camName;
xForm.parent = parent;
xForm.localPosition = Vector3.zero;
xForm.localPosition = position;
}
}
public struct CameraMountPoint
{
private Vector3 position;
private Transform xForm;
public Vector3 Position
{
get
{
return position;
}
set
{
position = value;
}
}
public Transform XForm
{
get
{
return xForm;
}
set
{
xForm = value;
}
}
public void Init(string camName, Vector3 pos, Transform transform, Transform parent)
{
position = pos;
xForm = transform;
xForm.name = camName;
xForm.parent = parent;
xForm.localPosition = Vector3.zero;
xForm.localPosition = position;
}
}
#endregion
}