为了方便存档,每种新手引导的Group和Guide都要赋值一个唯一的ID,这部分我们用自定义的工具来实现。

首先先在Guide——Business文件夹内分别新建Groups、Guides、Behaviours三个文件夹,这里面分别放置新手引导业务逻辑的各个脚本。

然后再在Guide内新建Tools文件夹和Tools——Editor文件夹,在其中新建GuideGenerateIDTool

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using UnityEditor;
using UnityEngine;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;

public class GuideGenerateIDTool
{
private static readonly string BUSINESS_FOLDER = Application.dataPath + "/Scripts/Guide/Bussiness/";
private static readonly string GROUP_FOLDER = BUSINESS_FOLDER + "Groups/";
private static readonly string GUIDE_FOLDER = BUSINESS_FOLDER + "Guides/";

private const string GROUP_REPLACE_CONTENT_OLD = "protected override int GroupID { get; }";
//字符串中一个大括号会被识别为匹配符,如果想要输出大括号为文字,需要两个大括号
private const string GROUP_REPLACE_CONTENT_NEW = "protected override int GroupID {{ get {{ return {0}; }} }}";
private const string GUIDE_REPLACE_CONTENT_OLD = "protected override int GuideID { get; }";
private const string GUIDE_REPLACE_CONTENT_NEW = "protected override int GuideID {{ get {{ return {0}; }} }}";

private static readonly string ID_DATA_KEY = "SaveIdLastOne";

[MenuItem("Tools/生成新手引导部分ID代码")]
public static void GenerateId()
{
GenerateGroupId();
GenerateGuideId();
Debug.Log("完成ID替换");
}
[MenuItem("Tools/删除默认Guide存档")]
private static void DeleteDefaultGuide()
{
PlayerPrefs.DeleteKey("0");
}
private static int IdData
{
get { return PlayerPrefs.GetInt(ID_DATA_KEY, 0); }
set { PlayerPrefs.SetInt(ID_DATA_KEY, value); }
}
private static void GenerateGuideId()
{
ReplaceId(GROUP_FOLDER, GROUP_REPLACE_CONTENT_OLD, GROUP_REPLACE_CONTENT_NEW);
}

private static void GenerateGroupId()
{
ReplaceId(GUIDE_FOLDER, GUIDE_REPLACE_CONTENT_OLD, GUIDE_REPLACE_CONTENT_NEW);
}
//用来正则替换的逻辑
private static string ReplaceEvaluator(Match match, string oldContent, string newContent)
{
int id = IdData;
IdData++;
return match.Groups[0].Value.Replace(oldContent, string.Format(newContent, id));
}

private static void ReplaceId(string directoryPath, string oldContent, string newContent)
{
if (JudgeDirectory(directoryPath))
{
string filePath = "";
string content = "";
List<string> paths = new List<string>();
GetFilePaths(directoryPath, paths);

foreach (string file in paths)
{
if (file.Contains(".cs") && !file.Contains(".meta"))
{
filePath = file;
content = File.ReadAllText(filePath);
//使用正则来替换,因为只有正则有匹配{0}的方式
content = Regex.Replace(content, oldContent, match => ReplaceEvaluator(match, oldContent, newContent));
File.WriteAllText(filePath, content);
}
}
}
else
{
Debug.LogErrorFormat("请按照以下路径新建文件夹:{0}", directoryPath);
}
}

private static List<string> GetFilePaths(string rootPath, List<string> paths)
{
foreach (string file in Directory.GetFiles(rootPath, "*.cs"))
{
paths.Add(file);
}
foreach (string directory in Directory.GetDirectories(rootPath))
{
GetFilePaths(directory, paths);
}
return paths;
}

private static bool JudgeDirectory(string path) => Directory.Exists(path);
}