我试图了解 UNet 以及如何分配客户端权限,但感觉有点失落。这是我想要完成的事情:
1) With On_LongTapEnd
= 创建一个容器,即多个对象(Orange" 和 "Apple")
2) 将“指针”作为父级并将 Orange 和 Apple 分配为子级
3)AssignClientAuthority()
到橙色和苹果,这样我就可以同步容器的运动(指针,橙色和苹果)
我不明白如何在这种情况下将客户端权限分配给 Orange 和 Apple,以便我可以移动容器。
我确实收到以下错误,请参阅代码中标记为 ERROR 的行:
Apple(Clone) (UnityEngine.GameObject) 所有者的 AssignClientAuthority 不能为空。请改用 RemoveClientAuthority()。
如果有人能指导我找到解决方案并描述我所缺少的,我将不胜感激。
这是代码:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using System.Collections.Generic;
public class Pointer : NetworkBehaviour {
public Transform myTransform;
public HashSet<GameObject> linecast_GameObject_HashSet = new HashSet<GameObject> ();
private List<GameObject> linecast_GameObject_List = new List<GameObject> ();
private GameObject parentGameObject;
private GameObject childGameObject;
private bool firstRun = true;
// Subscribe to events
void OnEnable(){
EasyTouch.On_TouchStart += On_TouchStart;
EasyTouch.On_Drag += On_Drag;
EasyTouch.On_DragEnd += On_DragEnd;
EasyTouch.On_LongTapStart += On_LongTapStart;
EasyTouch.On_LongTap += On_LongTap;
EasyTouch.On_LongTapEnd += On_LongTapEnd;
}
// Unsubscribe
void OnDisable(){
EasyTouch.On_TouchStart -= On_TouchStart;
EasyTouch.On_Drag -= On_Drag;
EasyTouch.On_DragEnd -= On_DragEnd;
EasyTouch.On_LongTapStart -= On_LongTapStart;
EasyTouch.On_LongTap -= On_LongTap;
EasyTouch.On_LongTapEnd -= On_LongTapEnd;
}
// Unsubscribe
void OnDestroy(){
OnDisable ();
//EasyTouch.On_TouchStart -= On_TouchStart;
}
// Touch start event
public void On_TouchStart(Gesture gesture){
// Debug.Log( "Touch in " + gesture.position);
if (!isLocalPlayer)
return;
myTransform.position = Camera.main.ScreenToWorldPoint (new Vector3 (gesture.position.x, gesture.position.y, 1f));
}
public void On_LongTapStart (Gesture gesture) {
linecast_GameObject_HashSet.Clear ();
}
public void On_LongTap(Gesture gesture) {
if (!isLocalPlayer)
return;
HashSet<GameObject> myList = new HashSet<GameObject> ();
myList = DoLinecast ();
}
public void On_LongTapEnd(Gesture gesture) {
if (!isLocalPlayer)
return;
if (linecast_GameObject_HashSet.Count > 0) {
foreach (GameObject aGO in linecast_GameObject_HashSet) {
childGameObject = GameObject.FindWithTag (aGO.tag);
childGameObject.GetComponent<Renderer> ().material.color = Color.green;
childGameObject.transform.parent = myTransform.transform;
// Assign Network Authority
NetworkIdentity myNetID = childGameObject.GetComponent<NetworkIdentity>();
print ("GO: " + childGameObject.tag + " // myNetID: " + myNetID.netId);
CmdAssignNetworkAuthority (myNetID.netId);
}
}
//CmdDoStuff ();
}
[Command]
public void CmdAssignNetworkAuthority (NetworkInstanceId toId) {
print ("Incoming ID: " + toId);
GameObject client = NetworkServer.FindLocalObject (toId);
var conn = client.GetComponent<NetworkIdentity> ().connectionToClient;
NetworkIdentity ni = client.GetComponent<NetworkIdentity> ();
if (ni.clientAuthorityOwner != null) {
// Remove previous owner
ni.RemoveClientAuthority (ni.clientAuthorityOwner);
print ("!= : " + ni.netId + " // name: " + ni.name);
} else if (ni.clientAuthorityOwner == null) {
print ("== : " + ni.netId + " // name: " + ni.name);
//============= ERROR ===============//
//============= ERROR ===============//
//============= ERROR ===============//
ni.AssignClientAuthority (conn);
// ==================================//
}
}
public void On_Drag(Gesture gesture) {
if (!isLocalPlayer)
return;
if (isLocalPlayer) {
gesture.pickedObject.transform.position = Camera.main.ScreenToWorldPoint (new Vector3 (gesture.position.x, gesture.position.y, 1f));
}
}
HashSet<GameObject> DoLinecast () {
GameObject linecast_GameObject;
LayerMask theLayer;
theLayer = (1 << LayerMask.NameToLayer("Fruit")); //set the layer to be clickable
Vector2 clickedPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
//Get current worldspace position of mouse cursor
RaycastHit2D[] hits = Physics2D.LinecastAll(clickedPos,clickedPos,theLayer);
foreach (RaycastHit2D ray in hits) {
linecast_GameObject = GameObject.Find(ray.collider.name);
linecast_GameObject_HashSet.Add (linecast_GameObject);
}
return linecast_GameObject_HashSet;
}
public void On_DragEnd (Gesture gesture) {
if (linecast_GameObject_HashSet.Count > 0) {
foreach (GameObject aGO in linecast_GameObject_HashSet) {
childGameObject = GameObject.FindWithTag (aGO.tag);
childGameObject.GetComponent<Renderer> ().material.color = Color.white;
childGameObject.transform.parent = null;
}
}
}
[Command]
public void CmdDoPrint(int xx, GameObject aGO) {
print (xx + ") Ray: " + aGO.name);
}
[Command]
public void CmdBIG() {
print (">>>BIGGER<<<");
}
[Command]
void CmdDoStuff() {
print ("Tap End");
}
}