using UnityEngine;
[CreateAssetMenu(fileName = "BulletData", menuName = "ScriptableObject/BulletData", order = 1)]
public class BulletData : ScriptableObject {
public float speed;
public float damage;
}
using UnityEngine;
public class BulletSpawner : MonoBehaviour {
public BulletData bulletData;
void Start() {
// 使用bulletData中的数据生成子弹
}
}
int
)、浮点数(float
)和字符串(string
)类型的数据。// 存储整数
PlayerPrefs.SetInt("Score", 100);
// 存储浮点数
PlayerPrefs.SetFloat("Volume", 0.5f);
// 存储字符串
PlayerPrefs.SetString("PlayerName", "John");
// 读取整数,如果不存在则返回默认值 -1
int score = PlayerPrefs.GetInt("Score", -1);
// 读取浮点数,如果不存在则返回默认值 0.0f
float volume = PlayerPrefs.GetFloat("Volume", 0.0f);
// 读取字符串,如果不存在则返回默认值 ""
string playerName = PlayerPrefs.GetString("PlayerName", "");
System.Xml
命名空间。假设我们要创建一个简单的 XML 文件来存储游戏角色信息。using System.Xml;
using UnityEngine;
public class CreateXMLExample : MonoBehaviour
{
void Start()
{
XmlDocument xmlDoc = new XmlDocument();
// 创建根节点
XmlElement root = xmlDoc.CreateElement("Characters");
xmlDoc.AppendChild(root);
// 创建角色节点
XmlElement character = xmlDoc.CreateElement("Character");
root.AppendChild(character);
// 创建角色属性节点
XmlElement name = xmlDoc.CreateElement("Name");
name.InnerText = "Warrior";
character.AppendChild(name);
XmlElement health = xmlDoc.CreateElement("Health");
health.InnerText = "100";
character.AppendChild(health);
// 保存XML文件
xmlDoc.Save(Application.dataPath + "/Characters.xml");
}
}
using System.Xml;
using UnityEngine;
public class ReadXMLExample : MonoBehaviour
{
void Start()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Application.dataPath + "/Characters.xml");
XmlNodeList characterNodes = xmlDoc.SelectNodes("/Characters/Character");
foreach (XmlNode characterNode in characterNodes)
{
XmlNode nameNode = characterNode.SelectSingleNode("Name");
XmlNode healthNode = characterNode.SelectSingleNode("Health");
if (nameNode != null && healthNode != null)
{
Debug.Log("Name: " + nameNode.InnerText + ", Health: " + healthNode.InnerText);
}
}
}
}
using System.Collections;
using UnityEngine;
using System.IO;
public static class SaveSystem//存档系统;
{
#region 存档
//存储一个单独的类;
public static void MySaveByJson(string saveFileName, object data)
{
string JsonStr = JsonUtility.ToJson(data);
string path = Path.Combine(Application.persistentDataPath, saveFileName);
try
{
File.WriteAllText(path, JsonStr);
Debug.Log($"成功将数据保存到:{path} !");
}
catch (System.Exception one)
{
Debug.LogError($"在{path}保存数据失败!\nbecause of{one}");
}
}
//存档,用于存储 结构体/类 数组
public static void MySaveByJsonMany(string saveFileName, object[] data)
{
string[] JsonStr = new string[data.Length];
for (int i = 0; i < data.Length; i++)
{
JsonStr[i] = JsonUtility.ToJson(data[i]);
}
string path = Path.Combine(Application.persistentDataPath, saveFileName);
try
{
File.WriteAllLines(path, JsonStr);
Debug.Log($"成功将数据保存到:{path} !");
}
catch (System.Exception one)
{
Debug.LogError($"在{path}保存数据失败!\nbecause of{one}");
}
}
#endregion
#region 读档
public static T MyReadFromJson(string saveFileName)
{
string path = Path.Combine(Application.persistentDataPath, saveFileName);
try
{
string JsonStr = File.ReadAllText(path);
T data = JsonUtility.FromJson(JsonStr);
Debug.Log($"成功从{path}读取到数据 !");
return data;
}
catch (System.Exception one)
{
Debug.LogError($"从{path}加载数据失败!,\nbecause of{one}");
return default;
}
}
public static T[] MyReadFromJsonMany(string saveFileName)
{
string path = Path.Combine(Application.persistentDataPath, saveFileName);
try
{
string[] JsonStr = File.ReadAllLines(path);
T[] data = new T[JsonStr.Length];
for (int i = 0; i < JsonStr.Length; i++)
{
data[i] = JsonUtility.FromJson(JsonStr[i]);
}
Debug.Log($"成功从{path}读取到数据 !");
return data;
}
catch (System.Exception one)
{
Debug.LogError($"从{path}加载数据失败!,\nbecause of{one}");
return default;
}
}
#endregion
#region 删档
public static void DeleteSaveData(string saveFileName)
{
string path = Path.Combine(Application.persistentDataPath, saveFileName);
try
{
File.Delete(path);
}
catch (System.Exception one)
{
Debug.LogError($"{path}路径的存档删除失败!\nbecause of:{one}");
}
}
#endregion
}
1.环境配置教程:参考链接C#调用MySQL数据库(使用MySql.Data.dll连接)-CSDN博客
2.常见错误以及解决方法:参考链接:unity 与数据库mysql连接的注意事项-CSDN博客
3.mysql在unity中的使用方法:参考链接:C# 的 MySql.Data.MySqlClient 库介绍:在 .NET 应用程序中与 MySQL 数据库进行交互-CSDN博客
using UnityEngine;
using MySql.Data.MySqlClient;
public class MySQLExample : MonoBehaviour
{
void Start()
{
// 数据库连接字符串,需根据实际情况修改
string connectionString = "server=localhost;user id=root;password=123456;database=test";
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
try
{
connection.Open();
Debug.Log("数据库连接成功");
string query = "SELECT * FROM your_table";
using (MySqlCommand command = new MySqlCommand(query, connection))
{
using (MySqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
// 读取数据,这里假设表中有name和age字段
string name = reader.GetString("name");
int age = reader.GetInt32("age");
Debug.Log("Name: " + name + ", Age: " + age);
}
}
}
}
catch (MySqlException ex)
{
Debug.LogError("数据库操作错误: " + ex.Message);
}
}
}
}
using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = "test.txt";
using (StreamWriter writer = new StreamWriter(filePath))
{
writer.WriteLine("这是第一行内容");
writer.WriteLine("这是第二行内容");
}
Console.WriteLine("写入成功");
}
}
using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = "test.txt";
string content = "这是要写入的内容";
File.WriteAllText(filePath, content);
Console.WriteLine("写入成功");
}
}
using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = "test.txt";
using (StreamReader reader = new StreamReader(filePath))
{
string line;
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
}
using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = "test.txt";
try
{
string content = File.ReadAllText(filePath);
Console.WriteLine(content);
}
catch (FileNotFoundException)
{
Console.WriteLine("文件未找到");
}
}
}
StreamWriter
和StreamReader
类用于逐行写入和读取文件内容;File.WriteAllText
和File.ReadAllText
方法则是一次性写入和读取整个文件内容。请根据实际需求选择合适的方式,同时注意处理可能出现的文件不存在等异常情况。