Unity3d 创建一个容器,然后使用AssignClientAuthority()
我试图理解UNet以及如何分配客户权限,但我感到有点迷失。以下是我试图实现的目标: 1) 使用LongTapEnd上的Unity3d 创建一个容器,然后使用AssignClientAuthority(),unity3d,multiplayer,unity-networking,Unity3d,Multiplayer,Unity Networking,我试图理解UNet以及如何分配客户权限,但我感到有点迷失。以下是我试图实现的目标: 1) 使用LongTapEnd上的=创建一个容器,即多个对象(橙色和苹果色) 2) 将“指针”作为父项,并将Orange和Apple指定为子项 3) AssignClientAuthority()到Orange和Apple,以便我可以同步容器(指针、Orange和Apple)的移动 我不明白如何在这种情况下为Orange和Apple分配客户端权限,以便移动容器 我确实收到了以下错误,请参见代码中的行标记错误: u
=创建一个容器,即多个对象(橙色和苹果色)
2) 将“指针”作为父项,并将Orange和Apple指定为子项
3) AssignClientAuthority()
到Orange和Apple,以便我可以同步容器(指针、Orange和Apple)的移动
我不明白如何在这种情况下为Orange和Apple分配客户端权限,以便移动容器
我确实收到了以下错误,请参见代码中的行标记错误:
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");
}
}
Apple(Clone)(UnityEngine.GameObject)所有者的AssignClientAuthority不能为null。请改用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");
}
}
使用UnityEngine;
使用系统集合;
使用UnityEngine。联网;
使用System.Collections.Generic;
公共类指针:NetworkBehavior{
公共转型;
public HashSet linecast_GameObject_HashSet=new HashSet();
私有列表linecast_GameObject_List=新列表();
私有游戏对象父游戏对象;
私有游戏对象子游戏对象;
private bool firstRun=true;
//订阅活动
void OnEnable(){
EasyTouch.On\u TouchStart+=On\u TouchStart;
EasyTouch.On_Drag+=On_Drag;
EasyTouch.On\u DragEnd+=On\u DragEnd;
EasyTouch.On_LongTapStart+=On_LongTapStart;
EasyTouch.On_LongTap+=On_LongTap;
EasyTouch.On_LongTapEnd+=On_LongTapEnd;
}
//退订
无效可禁用(){
EasyTouch.On\u TouchStart-=On\u TouchStart;
EasyTouch.On_Drag-=On_Drag;
EasyTouch.On\u DragEnd-=On\u DragEnd;
EasyTouch.On_LongTapStart-=On_LongTapStart;
EasyTouch.On_LongTap-=On_LongTap;
EasyTouch.On_LongTapEnd-=On_LongTapEnd;
}
//退订
void OnDestroy(){
OnDisable();
//EasyTouch.On\u TouchStart-=On\u TouchStart;
}
//触摸启动事件
TouchStart上的公共空白(手势){
//调试日志(“触摸”+手势位置);
如果(!isLocalPlayer)
返回;
myTransform.position=Camera.main.ScreenToWorldPoint(新矢量3(signature.position.x,signature.position.y,1f));
}
_LongTapStart上的公共空白(手势){
linecast_GameObject_HashSet.Clear();
}
_LongTap上的公共空白(手势){
如果(!isLocalPlayer)
返回;
HashSet myList=newhashset();
myList=DoLinecast();
}
龙潭公共空间(手势){
如果(!isLocalPlayer)
返回;
如果(linecast\u GameObject\u HashSet.Count>0){
foreach(linecast\u GameObject\u HashSet中的GameObject aGO){
childGameObject=GameObject.FindWithTag(aGO.tag);
childGameObject.GetComponent().material.color=color.green;
childGameObject.transform.parent=myTransform.transform;
//分配网络权限
NetworkIdentity myNetID=childGameObject.GetComponent();
打印(“GO:+childGameObject.tag+”//myNetID:+myNetID.netId);
CmdAssignNetworkAuthority(myNetID.netId);
}
}
//CmdDoStuff();
}
[命令]
public void CmdAssignNetworkAuthority(NetworkInstanceId toId){
打印(“传入ID:+toId”);
GameObject客户端=NetworkServer.FindLocalObject(toId);
var conn=client.GetComponent().connectionClient;
NetworkIdentity ni=client.GetComponent();
if(ni.clientAuthorityOwner!=null){
//删除以前的所有者
不可撤销客户权限(不可撤销客户权限所有者);
打印(!=:“+ni.netId+”//名称:“+ni.name”);
}else if(ni.clientAuthorityOwner==null){
打印(“==:”+ni.netId+“//名称:”+ni.name);
//=============错误===============//
//=============错误===============//
//=============错误===============//
指定客户授权(康涅狄格州);
// ==================================//
}
}
拖动时的公共空白(手势){
如果(!isLocalPlayer)
返回;
if(IsLocalLayer){
signature.pickedObject.transform.position=Camera.main.ScreenToWorldPoint(新矢量3(signature.position.x,signature.position.y,1f));
}
}
HashSet DoLinecast(){
GameObject linecast_GameObject;
图层屏蔽图层;
图层=(10){
foreach(linecast\u GameObject\u HashSet中的GameObject aGO){
childGameObject=GameObject.FindWithTag(aGO.tag);
childGameObject.GetComponent().material.color=color.white;
childGameObject.transform.parent=null;
}
}
}
[命令]
public void CmdDoPrint(int xx,游戏对象前){
打印(xx+”)射线:“+aGO.name”;
}
[命令]
公共空间{
打印(“>>>更大