日期:2014-05-17  浏览次数:21176 次

unity3d 一个c#类如何调用另一个c#类中的属性和方法

1.  

在一个c#类中调用类“PlayerManager”中的方法 : PlayerManager.Instance.SyncAnimation(userId, "Shot", 1);

在PlayerManager类中

public static PlayerManager Instance {
  get {
        return instance;
  }
 }

 void Awake() {
  instance = this;
 }

 

2.

在一个c#类中调用类“NetworkTransform”中的方法    NetworkTransform.FromTransform(thisTransform);

在“NetworkTransform”类中:

 public static NetworkTransform FromTransform(Transform transform) {
  NetworkTransform trans = new NetworkTransform();
    
  trans.position = transform.position;
  trans.angleRotation = transform.localEulerAngles;
    
  return trans;
 }

 

3.

         ScriptName other;
         other = gameObject.GetComponent("ScriptName") as ScriptName;
         other.DoSomething();

 

4.通过SendMessage方法调用


private void CheckRaycastWithEnemy() {
  Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
  RaycastHit hit;
  if (Physics.Raycast (ray, out hit, Mathf.Infinity, 1<<(int)GameLayers.TargetLayer)) {
   hit.collider.SendMessage("RaycastMessage", SendMessageOptions.DontRequireReceiver);
   
  }
 } 

在Enemy类中有方法“RaycastMessage”:

void RaycastMessage() {
  timeSinceLastRaycast = 0;
 }