问题描述
嗨,我想通过网络同步颜色和帽子,本地播放器运行良好。
enter code here
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using Photon.Pun;
public class AU_ColorCustomizer: MonoBehavIoUr
{
[Serializefield] Color[] allColors;
[Serializefield] Sprite[] allHats;
[Serializefield] GameObject colorPanel;
[Serializefield] GameObject hatPanel;
[Serializefield] Button colorTabButton;
[Serializefield] Button hatTabButton;
public void SetColor(int colorIndex)
{
AU_PlayerController.localPlayer.SetColor(allColors[colorIndex]);
}
public void SetHat(int hatIndex)
{
AU_PlayerController.localPlayer.SetHat(allHats[hatIndex]);
}
public void NextScene(int sceneIndex)
{
SceneManager.LoadScene(sceneIndex);
}
public void EnableColors()
{
colorPanel.SetActive(true);
hatPanel.SetActive(false);
colorTabButton.interactable = false;
hatTabButton.interactable = true;
}
public void EnableHats()
{
colorPanel.SetActive(false);
hatPanel.SetActive(true);
colorTabButton.interactable = true;
hatTabButton.interactable = false;
}
}
enter code here
using System.Collections.Generic;
using Photon.Pun;
using UnityEngine;
using UnityEngine.InputSystem;
using System.IO;
public class AU_PlayerController : MonoBehavIoUr,IPunObservable
{
public static AU_PlayerController localPlayer;
Rigidbody myRB;
Transform myAvatar;
Vector2 movementInput;
[Serializefield] InputAction WASD;
[Serializefield] float movementSpeed;
Animator myAnim;
static Color myColor;
SpriteRenderer myAvatarSprite;
static Sprite myHatSprite;
SpriteRenderer myHatHolder;
[Serializefield] bool isImposter;
[Serializefield] InputAction KILL;
[Serializefield] Collider myCollider;
bool isDead;
List<AU_PlayerController> targets;
[Serializefield] GameObject bodyPrefab;
public static List<Transform> allBodies;
List<Transform> bodiesFound;
[Serializefield] InputAction REPORT;
[Serializefield] LayerMask ignoreForBody;
[Serializefield] InputAction MOUSE;
Vector2 mousePositionInput;
Camera myCamera;
[Serializefield] InputAction INteraCTION;
[Serializefield] LayerMask interactLayer;
//Photon Variables
[Serializefield] GameObject lightMask;
PhotonView myPV;
float direction = 1;
private void Awake()
{
INteraCTION.performed += Interact;
KILL.performed += KillTarget;
REPORT.performed += ReportBody;
}
private void OnEnable()
{
REPORT.Enable();
WASD.Enable();
KILL.Enable();
MOUSE.Enable();
INteraCTION.Enable();
}
private void Ondisable()
{
REPORT.disable();
WASD.disable();
KILL.disable();
MOUSE.disable();
INteraCTION.disable();
}
// Start is called before the first frame update
void Start()
{
myPV = GetComponent<PhotonView>();
if (myPV.Ismine)
{
localPlayer = this;
}
myCamera = transform.GetChild(1).GetComponent<Camera>();
targets = new List<AU_PlayerController>();
myRB = GetComponent<Rigidbody>();
myAvatar = transform.GetChild(0);
myAnim = GetComponent<Animator>();
myAvatarSprite = myAvatar.GetComponent<SpriteRenderer>();
myHatHolder = myAvatar.GetChild(1).GetComponent<SpriteRenderer>();
if (!myPV.Ismine)
{
myCamera.gameObject.SetActive(false);
lightMask.SetActive(false);
return;
}
if (myColor == Color.clear)
myColor = Color.white;
myAvatarSprite.color = myColor;
allBodies = new List<Transform>();
bodiesFound = new List<Transform>();
if (myHatSprite != null)
myHatHolder.sprite = myHatSprite;
}
// Update is called once per frame
void Update()
{
myAvatar.localScale = new Vector2(direction,1);
if (!myPV.Ismine)
return;
movementInput = WASD.ReadValue<Vector2>();
myAnim.SetFloat("Speed",movementInput.magnitude);
if (movementInput.x != 0)
{
direction = Mathf.Sign(movementInput.x);
}
if(allBodies.Count > 0)
{
BodySearch();
}
mousePositionInput = MOUSE.ReadValue<Vector2>();
}
private void FixedUpdate()
{
if (!myPV.Ismine)
return;
myRB.veLocity = movementInput * movementSpeed;
}
public void SetColor(Color newColor)
{
myColor = newColor;
if(myAvatarSprite != null)
{
myAvatarSprite.color = myColor;
}
}
public void SetHat(Sprite newHat)
{
myHatSprite = newHat;
myHatHolder.sprite = myHatSprite;
}
public void SetRole(bool newRole)
{
isImposter = newRole;
}
private void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{
AU_PlayerController tempTarget = other.GetComponent<AU_PlayerController>();
if (isImposter)
{
if (tempTarget.isImposter)
return;
else
{
targets.Add( tempTarget);
}
}
}
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
AU_PlayerController tempTarget = other.GetComponent<AU_PlayerController>();
if (targets.Contains(tempTarget))
{
targets.Remove(tempTarget);
}
}
}
void KillTarget(InputAction.CallbackContext context)
{
if (!myPV.Ismine)
return;
if (!isImposter)
return;
if(context.phase == InputActionPhase.Performed)
{
if (targets.Count == 0)
return;
else
{
if (targets[targets.Count - 1].isDead)
return;
transform.position = targets[targets.Count - 1].transform.position;
//targets[targets.Count - 1].Die();
targets[targets.Count - 1].myPV.RPC("RPC_KILL",RpcTarget.All);
targets.RemoveAt(targets.Count - 1);
}
}
}
[PunRPC]
void RPC_KILL()
{
Die();
}
public void Die()
{
if (!myPV.Ismine)
return;
AU_Body tempBody = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs","AU_Body"),transform.position,transform.rotation).GetComponent<AU_Body>();
tempBody.SetColor(myAvatarSprite.color);
isDead = true;
myAnim.SetBool("isDead",isDead);
gameObject.layer = 8;
myCollider.enabled = false;
}
private void BodySearch()
{
foreach(Transform body in allBodies)
{
RaycastHit hit;
Ray ray = new Ray(transform.position,body.position - transform.position);
if (Physics.Raycast(ray,out hit,1000f,~ignoreForBody))
{
if(hit.transform == body)
{
if (bodiesFound.Contains(body.transform))
return;
bodiesFound.Add(body.transform);
}
else
{
bodiesFound.Remove(body.transform);
}
}
}
}
private void ReportBody(InputAction.CallbackContext obj)
{
if (bodiesFound == null)
return;
if (bodiesFound.Count == 0)
return;
Transform tempBody = bodiesFound[bodiesFound.Count - 1];
allBodies.Remove(tempBody);
bodiesFound.Remove(tempBody);
tempBody.GetComponent<AU_Body>().Report();
}
void Interact (InputAction.CallbackContext context)
{
if (context.phase == InputActionPhase.Performed)
{
RaycastHit hit;
Ray ray = myCamera.ScreenPointToRay(mousePositionInput);
if(Physics.Raycast(ray,interactLayer))
{
if (hit.transform.tag == "Interactable")
{
if (!hit.transform.GetChild(0).gameObject.activeInHierarchy)
return;
AU_Interactable temp = hit.transform.GetComponent<AU_Interactable>();
temp.PlayMiniGame();
}
if (hit.transform.tag == "Vent")
{
myAnim.SetBool("Vent",true);
AU_Interactable temp = hit.transform.GetComponent<AU_Interactable>();
temp.PlayMiniGame();
}
}
}
}
public void ExitVent()
{
myAnim.SetBool("Vent",false);
}
public void OnPhotonSerializeView(PhotonStream stream,PhotonMessageInfo info)
{
if (stream.IsWriting)
{
stream.SendNext(direction);
stream.SendNext(isImposter);
}
else
{
this.direction = (float)stream.ReceiveNext();
this.isImposter = (bool)stream.ReceiveNext();
}
}
public void BecomeImposter(int ImposterNumber)
{
if(PhotonNetwork.LocalPlayer == PhotonNetwork.PlayerList[ImposterNumber])
{
isImposter = true;
}
}
}
``` enter code here
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)