看了B站傅老师的视频,一边看一边做,好久没用很多的C#脚本来开发游戏了,做完后做一个小总结。
demo实现了:
1.剧情人物对白的展示
2.人物以及对白的动态配置
3.简单的FSM来控制游戏的流程
4.分支剧情
5.剧情触发游戏事件
开发流程;
1.对白人物的显示控制
public void ShowCanvas(bool canvasDisplay)
{
canvas.gameObject.SetActive(canvasDisplay);
}
public void ShowCharaA(bool value)
{
charaAImg.gameObject.SetActive(value);
}
public void ShowCharaB(bool value)
{
charaBImg.gameObject.SetActive(value);
}
public void ShowContentBg(bool value)
{
contentBg.gameObject.SetActive(value);
}
public void ShowContent(bool value)
{
content.gameObject.SetActive(value);
}
public void SetContent(string value)
{
content.text = value;
}
2.动态的配置文本头像
最开始写在脚本中,之后用ScriptableObject 中间使用到QuickSheet讲一个excel转为ScriptableObject
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
///
/// !!! Machine generated code !!!
///
/// A class which deriveds ScritableObject class so all its data
/// can be serialized onto an asset data file.
///
[System.Serializable]
public class story01 : ScriptableObject
{
[HideInInspector] [SerializeField]
public string SheetName = "";
[HideInInspector] [SerializeField]
public string WorksheetName = "";
// Note: initialize in OnEnable() not here.
public story01Data[] dataArray;
public List<story01Data> dataList;
void OnEnable()
{
//#if UNITY_EDITOR
//hideFlags = HideFlags.DontSave;
//#endif
// Important:
// It should be checked an initialization of any collection data before it is initialized.
// Without this check, the array collection which already has its data get to be null
// because OnEnable is called whenever Unity builds.
//
if (dataArray == null)
dataArray = new story01Data[0];
}
//
// Highly recommand to use LINQ to query the data sources.
//
}
using UnityEngine;
using System.Collections;
///
/// !!! Machine generated code !!!
/// !!! DO NOT CHANGE Tabs to Spaces !!!
///
[System.Serializable]
public class story01Data
{
[SerializeField]
string dialogtext;
public string Dialogtext { get {return dialogtext; } set { dialogtext = value;} }
[SerializeField]
bool charaadisplay;
public bool Charaadisplay { get {return charaadisplay; } set { charaadisplay = value;} }
[SerializeField]
bool charabdisplay;
public bool Charabdisplay { get {return charabdisplay; } set { charabdisplay = value;} }
[SerializeField]
bool ischoice;
public bool Ischoice { get {return ischoice; } set { ischoice = value;} }
[SerializeField]
string btnatext;
public string Btnatext { get {return btnatext; } set { btnatext = value;} }
[SerializeField]
string btnamsg;
public string Btnamsg { get {return btnamsg; } set { btnamsg = value;} }
[SerializeField]
string btnbtext;
public string Btnbtext { get {return btnbtext; } set { btnbtext = value;} }
[SerializeField]
string btnbmsg;
public string Btnbmsg { get {return btnbmsg; } set { btnbmsg = value;} }
}
中间生成其他剧情配置时候,直接修改了上面的sheet,生成新的ScriptableObject
3.简单的fsm来控制游戏的流程
void Update () {
switch (state)
{
case STATE.OFF:
if (firstEnter)
{
Init();
ShowUI();
firstEnter = false;
}
break;
case STATE.TYPING:
if (firstEnter)
{
LoadContent(data.dataList[curLine].Dialogtext, data.dataList[curLine].Charaadisplay, data.dataList[curLine].Charabdisplay);
firstEnter = false;
timeValue = 0;
}
UpdateContentString();
CheckTypingFinished();
break;
case STATE.PAUSED:
if (firstEnter)
{
firstEnter = false;
}
break;
case STATE.CHOICE:
if (firstEnter)
{
uiPanel.ShowBtns(true);
uiPanel.SetBtnText(data.dataList[curLine].Btnatext, data.dataList[curLine].Btnbtext);
uiPanel.SetBtnName(data.dataList[curLine].Btnamsg, data.dataList[curLine].Btnbmsg);
firstEnter = false;
}
break;
default:
break;
}
}
4.模拟角色到达目标点出发剧情开始
public class MyTrigger : MonoBehaviour {
public story01 data;
public AVGAssetConfig team;
public UIManager manager;
private void OnTriggerEnter(Collider other)
{
manager.StartAVG(data, team);
}
}
5.实现点击按钮打开门的效果
void Update () {
transform.Translate(0, speed * Time.deltaTime, 0);
if(transform.position.y>=maxY)
{
transform.position = new Vector3(transform.position.x, maxY, transform.position.z);
}
}
6.不适用dotoween实现的打字效果
void UpdateContentString()
{
timeValue += Time.deltaTime;
string tempString = targetString.Substring(0, Mathf.Min((int)(Mathf.Floor(timeValue* typingSpeed)),targetString.Length));
uiPanel.SetContent(tempString);
}