Add Message Box & TimeLimit Change Button

Add Message Box & TimeLimit Change Button.
Start Using XXManager to keep script, now changed in UIManger GameObject.
This commit is contained in:
2022-09-07 06:48:37 +09:00
parent 885dbb92e9
commit 1aaf6c7069
17 changed files with 2384 additions and 331 deletions
@@ -90,7 +90,8 @@ public class AgentWithGun : Agent
[System.NonSerialized] public float loseReward;
[System.NonSerialized] public float killReward;
[System.NonSerialized] public float saveNow = 0;
[System.NonSerialized] public int remainTime;
void Start()
{
@@ -102,6 +103,9 @@ public class AgentWithGun : Agent
// Enemy Num
enemyNum = DataTransfer.EnemyNum;
// Time Limit
timeLimit = DataTransfer.Timelim;
// get load directory.
LoadDirDate = DataTransfer.LoadDirDate;
LoadDirTime = DataTransfer.LoadDirTime;
@@ -129,7 +133,10 @@ public class AgentWithGun : Agent
// change Decision Period & Take Actions Between Decisions
transform.GetComponent<DecisionRequester>().DecisionPeriod = DataTransfer.DecisionPeriod;
transform.GetComponent<DecisionRequester>().TakeActionsBetweenDecisions = DataTransfer.ActionsBetweenDecisions;
}
//initialize remainTime
remainTime = (int)(timeLimit - Time.time + startTime);
}
/* ----------此Update用于debugBuild前删除或注释掉!----------*/
/*void Update()
@@ -528,7 +535,7 @@ public class AgentWithGun : Agent
// loseRewardIn = actionBuffers.ContinuousActions[6];
//float killRewardIn = actionBuffers.ContinuousActions[7];
//Rewards Update
int remainTime = (int)(timeLimit - Time.time + startTime);
remainTime = (int)(timeLimit - Time.time + startTime);
//应用输入
shoot = mouseShoot;
@@ -0,0 +1,81 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InGameMessages : MonoBehaviour
{
public GameObject MessageBox; // scrollView contents
public GameObject TextPrefab; // TextPrefab
public int maxMassages = 25;
public Color infoColor = Color.white;
public Color successColor = Color.green;
public Color errorColor = Color.red;
public Color warningColor = Color.yellow;
[System.NonSerialized]List<Message> messageList = new List<Message>();
public void SendMessagetoBox(string text,Message.MessageType messageType = Message.MessageType.info)
{
if (messageList.Count > maxMassages)
{
// keep mesages under maxMassages
Destroy(messageList[0].textOBJ.gameObject);
messageList.Remove(messageList[0]);
}
// add timestamp
string date = "[" + DateTime.Now.ToString("MMdd_hh:mm:ss") + "] ";
text = date + text;
Message newMessage = new Message();
newMessage.text = text;
GameObject newText = Instantiate(TextPrefab, MessageBox.transform);
newMessage.textOBJ = newText.GetComponent<Text>();
newMessage.textOBJ.text = newMessage.text;
newMessage.textOBJ.color = MessageTypeColor(messageType);
messageList.Add(newMessage);
}
// parse messageType to Color
Color MessageTypeColor(Message.MessageType messageType)
{
Color thisColor = infoColor;
switch (messageType)
{
case Message.MessageType.info:
thisColor = infoColor;
break;
case Message.MessageType.success:
thisColor = successColor;
break;
case Message.MessageType.error:
thisColor = errorColor;
break;
case Message.MessageType.warnning:
thisColor = warningColor;
break;
}
return thisColor;
}
}
// Message Class
[System.Serializable]
public class Message
{
public string text;
public Text textOBJ;
public MessageType messageType;
public enum MessageType
{
info,
success,
error,
warnning
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 414edca99b7a04940a0801ec06c99007
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -8,21 +8,21 @@ public class RealTimeEnemyNumChanger : MonoBehaviour
public InputField enemyNumInputField;
public Text enemyNumPlaceholder;
public Text enemyNumAddInfoText;
private void Start()
public void EnemyChangeBTPresses()
{
enemyNumAddInfoText.text = "";
}
AgentWithGun agentWithGun = Agent.GetComponent<AgentWithGun>();
InGameMessages messenger = gameObject.GetComponent<InGameMessages>();
int enemyNum = Math.Abs(int.Parse(enemyNumInputField.GetComponent<InputField>().text));
public void nonRBTPresses()
{
if (enemyNumInputField.GetComponent<InputField>().text == "-")
{
// input chara not illegal
enemyNumPlaceholder.color = Color.red;
enemyNumPlaceholder.text = "Wrong Type!";
enemyNumInputField.GetComponent<InputField>().text = "";
messenger.SendMessagetoBox("Wrong enemyNum Type!", Message.MessageType.error);
}
else if (enemyNumInputField.GetComponent<InputField>().text == "")
{
@@ -35,10 +35,9 @@ public class RealTimeEnemyNumChanger : MonoBehaviour
// good to go~
enemyNumPlaceholder.color = Color.gray;
enemyNumPlaceholder.text = "nonR";
string num = enemyNumInputField.GetComponent<InputField>().text;
Agent.GetComponent<AgentWithGun>().enemyNum = Math.Abs(int.Parse(num));
agentWithGun.enemyNum = enemyNum;
enemyNumInputField.GetComponent<InputField>().text = "";
enemyNumAddInfoText.text = $"Enemy Num = {num} add Success. Valid in the next round.";
messenger.SendMessagetoBox($"Enemy Num = {enemyNum} add Success. Valid in the next round.", Message.MessageType.success);
}
}
}
@@ -23,15 +23,23 @@ public class RealTimeRewardChanger : MonoBehaviour
public Text winRPlaceholder;
public Text loseRPlaceholder;
private void Start()
{
}
public void nonRBTPresses()
{
InGameMessages messenger = gameObject.GetComponent<InGameMessages>();
if (nonRInputField.GetComponent<InputField>().text == "-")
{
// input chara not illegal
nonRPlaceholder.color = Color.red;
nonRPlaceholder.text = "Wrong Type!";
nonRInputField.GetComponent<InputField>().text = "";
messenger.SendMessagetoBox("Wrong Reward Type!", Message.MessageType.error);
}else if (nonRInputField.GetComponent<InputField>().text == "")
{
// empty chara
@@ -46,17 +54,20 @@ public class RealTimeRewardChanger : MonoBehaviour
string reward = nonRInputField.GetComponent<InputField>().text;
Agent.GetComponent<AgentWithGun>().nonReward = float.Parse(reward);
nonRInputField.GetComponent<InputField>().text = "";
messenger.SendMessagetoBox("Reward nonR change Success",Message.MessageType.success);
}
}
public void shootRBTPresses()
{
InGameMessages messenger = gameObject.GetComponent<InGameMessages>();
if (shootRInputField.GetComponent<InputField>().text == "-")
{
// input chara not illegal
shootRPlaceholder.color = Color.red;
shootRPlaceholder.text = "Wrong Type!";
shootRInputField.GetComponent<InputField>().text = "";
messenger.SendMessagetoBox("Wrong Reward Type!", Message.MessageType.error);
}
else if (shootRInputField.GetComponent<InputField>().text == "")
{
@@ -72,17 +83,20 @@ public class RealTimeRewardChanger : MonoBehaviour
string reward = shootRInputField.GetComponent<InputField>().text;
Agent.GetComponent<AgentWithGun>().shootReward = float.Parse(reward);
shootRInputField.GetComponent<InputField>().text = "";
messenger.SendMessagetoBox("Reward shootR change Success",Message.MessageType.success);
}
}
public void shootWithoutReadyRBTPresses()
{
InGameMessages messenger = gameObject.GetComponent<InGameMessages>();
if (shootWithoutReadyRInputField.GetComponent<InputField>().text == "-")
{
// input chara not illegal
shootWithoutReadyRPlaceholder.color = Color.red;
shootWithoutReadyRPlaceholder.text = "Wrong Type!";
shootWithoutReadyRInputField.GetComponent<InputField>().text = "";
messenger.SendMessagetoBox("Wrong Reward Type!", Message.MessageType.error);
}
else if (shootWithoutReadyRInputField.GetComponent<InputField>().text == "")
{
@@ -98,17 +112,20 @@ public class RealTimeRewardChanger : MonoBehaviour
string reward = shootWithoutReadyRInputField.GetComponent<InputField>().text;
Agent.GetComponent<AgentWithGun>().shootWithoutReadyReward = float.Parse(reward);
shootWithoutReadyRInputField.GetComponent<InputField>().text = "";
messenger.SendMessagetoBox("Reward SWORR change Success",Message.MessageType.success);
}
}
public void hitRBTPresses()
{
InGameMessages messenger = gameObject.GetComponent<InGameMessages>();
if (hitRInputField.GetComponent<InputField>().text == "-")
{
// input chara not illegal
hitRPlaceholder.color = Color.red;
hitRPlaceholder.text = "Wrong Type!";
hitRInputField.GetComponent<InputField>().text = "";
messenger.SendMessagetoBox("Wrong Reward Type!", Message.MessageType.error);
}
else if (hitRInputField.GetComponent<InputField>().text == "")
{
@@ -124,17 +141,20 @@ public class RealTimeRewardChanger : MonoBehaviour
string reward = hitRInputField.GetComponent<InputField>().text;
Agent.GetComponent<AgentWithGun>().hitReward = float.Parse(reward);
hitRInputField.GetComponent<InputField>().text = "";
messenger.SendMessagetoBox("Reward hitR change Success",Message.MessageType.success);
}
}
public void killRBTPresses()
{
InGameMessages messenger = gameObject.GetComponent<InGameMessages>();
if (killRInputField.GetComponent<InputField>().text == "-")
{
// input chara not illegal
killRPlaceholder.color = Color.red;
killRPlaceholder.text = "Wrong Type!";
killRInputField.GetComponent<InputField>().text = "";
messenger.SendMessagetoBox("Wrong Reward Type!", Message.MessageType.error);
}
else if (killRInputField.GetComponent<InputField>().text == "")
{
@@ -150,17 +170,20 @@ public class RealTimeRewardChanger : MonoBehaviour
string reward = killRInputField.GetComponent<InputField>().text;
Agent.GetComponent<AgentWithGun>().killReward = float.Parse(reward);
killRInputField.GetComponent<InputField>().text = "";
messenger.SendMessagetoBox("Reward killR change Success",Message.MessageType.success);
}
}
public void winRBTPresses()
{
InGameMessages messenger = gameObject.GetComponent<InGameMessages>();
if (winRInputField.GetComponent<InputField>().text == "-")
{
// input chara not illegal
winRPlaceholder.color = Color.red;
winRPlaceholder.text = "Wrong Type!";
winRInputField.GetComponent<InputField>().text = "";
messenger.SendMessagetoBox("Wrong Reward Type!", Message.MessageType.error);
}
else if (winRInputField.GetComponent<InputField>().text == "")
{
@@ -176,17 +199,20 @@ public class RealTimeRewardChanger : MonoBehaviour
string reward = winRInputField.GetComponent<InputField>().text;
Agent.GetComponent<AgentWithGun>().winReward = float.Parse(reward);
winRInputField.GetComponent<InputField>().text = "";
messenger.SendMessagetoBox("Reward winR change Success", Message.MessageType.success);
}
}
public void loseRBTPresses()
{
InGameMessages messenger = gameObject.GetComponent<InGameMessages>();
if (loseRInputField.GetComponent<InputField>().text == "-")
{
// input chara not illegal
loseRPlaceholder.color = Color.red;
loseRPlaceholder.text = "Wrong Type!";
loseRInputField.GetComponent<InputField>().text = "";
messenger.SendMessagetoBox("Wrong Reward Type!", Message.MessageType.error);
}
else if (loseRInputField.GetComponent<InputField>().text == "")
{
@@ -202,6 +228,7 @@ public class RealTimeRewardChanger : MonoBehaviour
string reward = loseRInputField.GetComponent<InputField>().text;
Agent.GetComponent<AgentWithGun>().loseReward = float.Parse(reward);
loseRInputField.GetComponent<InputField>().text = "";
messenger.SendMessagetoBox("Reward loseR change Success",Message.MessageType.success);
}
}
}
@@ -0,0 +1,54 @@
using System;
using UnityEngine;
using UnityEngine.UI;
public class RealTimeTimeLimitChanger : MonoBehaviour
{
public GameObject Agent;
public InputField TimeLimInputField;
public Text TimeLimPlaceholder;
public void BTPressed()
{
AgentWithGun agentWithGun = Agent.GetComponent<AgentWithGun>();
InGameMessages messenger = gameObject.GetComponent<InGameMessages>();
int timeLimit = Math.Abs(int.Parse(TimeLimInputField.GetComponent<InputField>().text));
if (TimeLimInputField.GetComponent<InputField>().text == "-")
{
// input chara not illegal
TimeLimPlaceholder.color = Color.red;
TimeLimPlaceholder.text = "Wrong Type!";
TimeLimInputField.GetComponent<InputField>().text = "";
messenger.SendMessagetoBox("Wrong timeLimit Type!", Message.MessageType.error);
}
else if (TimeLimInputField.GetComponent<InputField>().text == "")
{
// empty chara
TimeLimPlaceholder.color = Color.gray;
TimeLimPlaceholder.text = "TimeLim";
}
else
{
int remainTime = agentWithGun.remainTime;
// make sure new timeLimit is greater than remainTime;
if (timeLimit <= remainTime)
{
TimeLimPlaceholder.color = Color.red;
TimeLimPlaceholder.text = "Error";
messenger.SendMessagetoBox($"New time should greater than remainTime({remainTime})",Message.MessageType.error);
}
else
{
// good to go~
TimeLimPlaceholder.color = Color.gray;
TimeLimPlaceholder.text = "TimeLim";
agentWithGun.timeLimit = timeLimit;
TimeLimInputField.GetComponent<InputField>().text = "";
messenger.SendMessagetoBox($"Time Limit changed to {timeLimit}",Message.MessageType.success);
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 604d05168c977c04887a3add62ce34da
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -15,7 +15,6 @@ public class StartSceneEnemyNumChanger : MonoBehaviour
if (EnemyNumInput.GetComponent<InputField>().text == "" || EnemyNumInput.GetComponent<InputField>().text.Contains("-"))
{
EnemyNumText.color = Color.gray;
DataTransfer.GetComponent<StartSeneData>().nonReward = DataTransfer.GetComponent<StartSeneData>().EnemyNumDefault;
}
else
{
@@ -0,0 +1,25 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class StartSceneTimeLimChanger : MonoBehaviour
{
public GameObject DataTransfer;
public Text TimeLimText;
public InputField TimelimInput;
public void onValueTimeChanged()
{
if (TimelimInput.GetComponent<InputField>().text == "" || TimelimInput.GetComponent<InputField>().text.Contains("-"))
{
TimeLimText.color = Color.gray;
}
else
{
TimeLimText.color = Color.yellow;
DataTransfer.GetComponent<StartSeneData>().Timelim = Math.Abs(int.Parse(TimelimInput.GetComponent<InputField>().text));
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7857e2b2e5caf6b4686c4a7d87fa998e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -19,21 +19,23 @@ public class StartSeneData : MonoBehaviour
[System.NonSerialized]public string LoadDirTime = "0";
// Rewards
[System.NonSerialized]public float nonReward;
[System.NonSerialized] public float nonReward;
[System.NonSerialized] public float shootReward;
[System.NonSerialized]public float shootWithoutReadyReward;
[System.NonSerialized]public float hitReward;
[System.NonSerialized]public float killReward;
[System.NonSerialized]public float winReward;
[System.NonSerialized]public float loseReward;
[System.NonSerialized] public float shootWithoutReadyReward;
[System.NonSerialized] public float hitReward;
[System.NonSerialized] public float killReward;
[System.NonSerialized] public float winReward;
[System.NonSerialized] public float loseReward;
// DecisionPeriod
[System.NonSerialized]public int DecisionPeriod = 1;
[System.NonSerialized]public bool ActionsBetweenDecisions = true;
[System.NonSerialized] public int DecisionPeriod = 1;
[System.NonSerialized] public bool ActionsBetweenDecisions = true;
// EnemyNum
public int EnemyNumDefault = 3;
[System.NonSerialized]public int EnemyNum = 3;
[System.NonSerialized] public int EnemyNum = 3;
// TimeLimit
[System.NonSerialized] public int Timelim = 30;
private void Start()
{