Made RandomColor a Separate Component (#1240)

* Made RandomColor a separate component

* Adjusted Prefab

* Simplified OnValidate

* Reverted unrelated changes
This commit is contained in:
MrGadget 2019-11-28 10:42:46 -05:00 committed by Paul Pacheco
parent 2bb65a6f64
commit d963e29d95
12 changed files with 214 additions and 128 deletions

View File

@ -65,7 +65,6 @@ MeshRenderer:
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
@ -90,8 +89,9 @@ GameObject:
- component: {fileID: 7482234573209362796}
- component: {fileID: 8972496075962964235}
- component: {fileID: 8993127209816276930}
- component: {fileID: 8537344390966522168}
- component: {fileID: 8704659178864205755}
- component: {fileID: 3086414693581178039}
- component: {fileID: 8537344390966522168}
- component: {fileID: 887491563423388292}
m_Layer: 8
m_Name: Player
@ -148,7 +148,6 @@ MeshRenderer:
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
@ -179,6 +178,49 @@ CharacterController:
m_SkinWidth: 0.08
m_MinMoveDistance: 0.001
m_Center: {x: 0, y: 0, z: 0}
--- !u!114 &8704659178864205755
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8872462076811691049}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: e8f68561248aaca4fb96847ce24742ee, type: 3}
m_Name:
m_EditorClassIdentifier:
syncMode: 0
syncInterval: 0
characterController: {fileID: 8993127209816276930}
moveSpeed: 8
turnSpeedAccel: 5
turnSpeedDecel: 5
maxTurnSpeed: 150
jumpSpeed: 0
jumpFactor: 0.025
horizontal: 0
vertical: 0
turn: 0
isGrounded: 1
isFalling: 0
--- !u!114 &3086414693581178039
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8872462076811691049}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 42d1f80407105ee4f960f0b51e89452d, type: 3}
m_Name:
m_EditorClassIdentifier:
syncMode: 0
syncInterval: 0.1
color:
serializedVersion: 2
rgba: 4278190080
--- !u!114 &8537344390966522168
MonoBehaviour:
m_ObjectHideFlags: 0
@ -194,31 +236,6 @@ MonoBehaviour:
serverOnly: 0
m_AssetId:
m_SceneId: 0
--- !u!114 &8704659178864205755
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8872462076811691049}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: e8f68561248aaca4fb96847ce24742ee, type: 3}
m_Name:
m_EditorClassIdentifier:
syncMode: 0
syncInterval: 0
moveSpeed: 8
turnSpeedAccel: 5
turnSpeedDecel: 5
maxTurnSpeed: 150
jumpSpeed: 0
jumpFactor: 0.025
horizontal: 0
vertical: 0
turn: 0
isGrounded: 1
isFalling: 0
--- !u!114 &887491563423388292
MonoBehaviour:
m_ObjectHideFlags: 0

View File

@ -5,40 +5,6 @@ namespace Mirror.Examples.Additive
[RequireComponent(typeof(CharacterController))]
public class PlayerController : NetworkBehaviour
{
public override void OnStartServer()
{
base.OnStartServer();
playerColor = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
}
[SyncVar(hook = nameof(SetColor))]
public Color32 playerColor = Color.black;
// Unity clones the material when GetComponent<Renderer>().material is called
// Cache it here and destroy it in OnDestroy to prevent a memory leak
Material cachedMaterial;
void SetColor(Color32 color)
{
if (cachedMaterial == null) cachedMaterial = GetComponent<Renderer>().material;
cachedMaterial.color = color;
}
void OnDestroy()
{
Destroy(cachedMaterial);
}
void OnDisable()
{
if (isLocalPlayer)
{
Camera.main.transform.SetParent(null);
Camera.main.transform.localPosition = new Vector3(0f, 50f, 0f);
Camera.main.transform.localEulerAngles = new Vector3(90f, 0f, 0f);
}
}
CharacterController characterController;
public override void OnStartLocalPlayer()
@ -52,6 +18,16 @@ public override void OnStartLocalPlayer()
Camera.main.transform.localEulerAngles = new Vector3(10f, 0f, 0f);
}
void OnDisable()
{
if (isLocalPlayer)
{
Camera.main.transform.SetParent(null);
Camera.main.transform.localPosition = new Vector3(0f, 50f, 0f);
Camera.main.transform.localEulerAngles = new Vector3(90f, 0f, 0f);
}
}
[Header("Movement Settings")]
public float moveSpeed = 8f;
public float turnSpeedAccel = 5f;

View File

@ -0,0 +1,32 @@
using UnityEngine;
namespace Mirror.Examples.Additive
{
public class RandomColor : NetworkBehaviour
{
public override void OnStartServer()
{
base.OnStartServer();
color = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
}
// Color32 packs to 4 bytes
[SyncVar(hook = nameof(SetColor))]
public Color32 color = Color.black;
// Unity clones the material when GetComponent<Renderer>().material is called
// Cache it here and destroy it in OnDestroy to prevent a memory leak
Material cachedMaterial;
void SetColor(Color32 newColor)
{
if (cachedMaterial == null) cachedMaterial = GetComponent<Renderer>().material;
cachedMaterial.color = newColor;
}
void OnDestroy()
{
Destroy(cachedMaterial);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 42d1f80407105ee4f960f0b51e89452d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -89,8 +89,9 @@ GameObject:
- component: {fileID: 33762848230157288}
- component: {fileID: 23313067197120050}
- component: {fileID: 143011667059871024}
- component: {fileID: 114402732107420660}
- component: {fileID: 114892629901890886}
- component: {fileID: 6261579163786439309}
- component: {fileID: 114402732107420660}
- component: {fileID: 114265392388239132}
m_Layer: 0
m_Name: GamePlayer
@ -177,21 +178,6 @@ CharacterController:
m_SkinWidth: 0.08
m_MinMoveDistance: 0.001
m_Center: {x: 0, y: 0, z: 0}
--- !u!114 &114402732107420660
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1480027675339556}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9b91ecbcc199f4492b9a91e820070131, type: 3}
m_Name:
m_EditorClassIdentifier:
serverOnly: 0
m_AssetId:
m_SceneId: 1991537313
--- !u!114 &114892629901890886
MonoBehaviour:
m_ObjectHideFlags: 0
@ -208,7 +194,7 @@ MonoBehaviour:
syncInterval: 0
index: 0
score: 0
playerColor: {r: 0, g: 0, b: 0, a: 1}
characterController: {fileID: 143011667059871024}
moveSpeed: 8
turnSpeedAccel: 5
turnSpeedDecel: 5
@ -220,6 +206,38 @@ MonoBehaviour:
turn: 0
isGrounded: 1
isFalling: 0
--- !u!114 &6261579163786439309
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1480027675339556}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 1e6a8bf08f02e254aa9a52ef0aaa1553, type: 3}
m_Name:
m_EditorClassIdentifier:
syncMode: 0
syncInterval: 0.1
color:
serializedVersion: 2
rgba: 4278190080
--- !u!114 &114402732107420660
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1480027675339556}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9b91ecbcc199f4492b9a91e820070131, type: 3}
m_Name:
m_EditorClassIdentifier:
serverOnly: 0
m_AssetId:
m_SceneId: 0
--- !u!114 &114265392388239132
MonoBehaviour:
m_ObjectHideFlags: 0

View File

@ -13,6 +13,7 @@ GameObject:
- component: {fileID: 23203154358629070}
- component: {fileID: 135606878775227198}
- component: {fileID: 114048121767222990}
- component: {fileID: 7669440687796875101}
- component: {fileID: 114251241889735402}
m_Layer: 0
m_Name: Prize
@ -107,9 +108,26 @@ MonoBehaviour:
m_EditorClassIdentifier:
syncMode: 0
syncInterval: 0.1
prizeColor: {r: 0, g: 0, b: 0, a: 1}
available: 1
spawner: {fileID: 0}
randomColor: {fileID: 7669440687796875101}
--- !u!114 &7669440687796875101
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1139254171913846}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 1e6a8bf08f02e254aa9a52ef0aaa1553, type: 3}
m_Name:
m_EditorClassIdentifier:
syncMode: 0
syncInterval: 0.1
color:
serializedVersion: 2
rgba: 4278190080
--- !u!114 &114251241889735402
MonoBehaviour:
m_ObjectHideFlags: 0
@ -124,4 +142,4 @@ MonoBehaviour:
m_EditorClassIdentifier:
serverOnly: 0
m_AssetId:
m_SceneId: 3180781472
m_SceneId: 0

View File

@ -18,7 +18,6 @@ public override bool OnRoomServerSceneLoadedForPlayer(GameObject roomPlayer, Gam
{
PlayerController player = gamePlayer.GetComponent<PlayerController>();
player.index = roomPlayer.GetComponent<NetworkRoomPlayer>().index;
player.playerColor = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
return true;
}

View File

@ -11,34 +11,6 @@ public class PlayerController : NetworkBehaviour
[SyncVar]
public uint score;
[SyncVar(hook = nameof(SetColor))]
public Color32 playerColor = Color.black;
// Unity clones the material when GetComponent<Renderer>().material is called
// Cache it here and destroy it in OnDestroy to prevent a memory leak
Material cachedMaterial;
void SetColor(Color32 color)
{
if (cachedMaterial == null) cachedMaterial = GetComponent<Renderer>().material;
cachedMaterial.color = color;
}
void OnDestroy()
{
Destroy(cachedMaterial);
}
void OnDisable()
{
if (isLocalPlayer)
{
Camera.main.transform.SetParent(null);
Camera.main.transform.localPosition = new Vector3(0f, 50f, 0f);
Camera.main.transform.localEulerAngles = new Vector3(90f, 0f, 0f);
}
}
CharacterController characterController;
public override void OnStartLocalPlayer()
@ -52,6 +24,16 @@ public override void OnStartLocalPlayer()
Camera.main.transform.localEulerAngles = new Vector3(10f, 0f, 0f);
}
void OnDisable()
{
if (isLocalPlayer)
{
Camera.main.transform.SetParent(null);
Camera.main.transform.localPosition = new Vector3(0f, 50f, 0f);
Camera.main.transform.localEulerAngles = new Vector3(90f, 0f, 0f);
}
}
[Header("Movement Settings")]
public float moveSpeed = 8f;
public float turnSpeedAccel = 5f;

View File

@ -0,0 +1,32 @@
using UnityEngine;
namespace Mirror.Examples.NetworkRoom
{
public class RandomColor : NetworkBehaviour
{
public override void OnStartServer()
{
base.OnStartServer();
color = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
}
// Color32 packs to 4 bytes
[SyncVar(hook = nameof(SetColor))]
public Color32 color = Color.black;
// Unity clones the material when GetComponent<Renderer>().material is called
// Cache it here and destroy it in OnDestroy to prevent a memory leak
Material cachedMaterial;
void SetColor(Color32 newColor)
{
if (cachedMaterial == null) cachedMaterial = GetComponent<Renderer>().material;
cachedMaterial.color = newColor;
}
void OnDestroy()
{
Destroy(cachedMaterial);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1e6a8bf08f02e254aa9a52ef0aaa1553
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -2,30 +2,21 @@
namespace Mirror.Examples.NetworkRoom
{
[RequireComponent(typeof(RandomColor))]
public class Reward : NetworkBehaviour
{
[SyncVar(hook = nameof(SetColor))]
public Color prizeColor = Color.black;
// Unity makes a clone of the material when GetComponent<Renderer>().material is used
// Cache it here and Destroy it in OnDestroy to prevent a memory leak
Material materialClone;
void SetColor(Color color)
{
if (materialClone == null) materialClone = GetComponent<Renderer>().material;
materialClone.color = color;
}
void OnDestroy()
{
Destroy(materialClone);
}
public bool available = true;
public Spawner spawner;
uint points;
public RandomColor randomColor;
void OnValidate()
{
if (randomColor == null)
randomColor = GetComponent<RandomColor>();
}
// This is called from PlayerController.CmdClaimPrize which is invoked by PlayerController.OnControllerColliderHit
// This only runs on the server
public void ClaimPrize(GameObject player)
@ -36,6 +27,8 @@ public void ClaimPrize(GameObject player)
// First hit turns it off, pending the object being destroyed a few frames later.
available = false;
Color prizeColor = randomColor.color;
// calculate the points from the color ... lighter scores higher as the average approaches 255
// UnityEngine.Color RGB values are float fractions of 255
points = (uint)(((prizeColor.r * 255) + (prizeColor.g * 255) + (prizeColor.b * 255)) / 3);

View File

@ -27,9 +27,6 @@ public void SpawnPrize()
newPrize.name = prizePrefab.name;
reward = newPrize.gameObject.GetComponent<Reward>();
reward.spawner = this;
reward.prizeColor = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
if (LogFilter.Debug) Debug.LogFormat("Spawning Prize R:{0} G:{1} B:{2}", reward.prizeColor.r, reward.prizeColor.g, reward.prizeColor.b);
NetworkServer.Spawn(newPrize);
}