201 lines
5.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "Setting", menuName = "Создание настроек/Создать настройку"), System.Serializable]
public class Setting : ScriptableObject
{
//general
[Header("General")]
public SettingType Type;
public string Id;
public string Alias;
[TextArea] public string Description;
//type: Bool
public bool BoolValue;
//type: Int
public int IntValue;
public int IntMin;
public int IntMax;
public int IntStep;
//type: Float
public float FloatValue;
public float FloatMin;
public float FloatMax;
public float FloatStep;
//type: String
public string StringValue;
public List<string> AllowedValues;
//type: Signal
public string SignalValue;
//Хэш не зависит от значения настройки. Иначе каждый раз будет перезагружать дефолтную схему
//Кроме сигнала. У него значение постоянно.
public string GetStringForHash()
{
string result = Id;
result += Alias;
result += Description;
switch (Type)
{
case SettingType.Bool:
break;
case SettingType.Int:
result += IntMin + IntMax + IntStep; break;
case SettingType.Float:
result += FloatMin + FloatMax + FloatStep; break;
case SettingType.String:
foreach (string s in AllowedValues) result += s; break;
case SettingType.Signal:
result += SignalValue; break;
}
return result;
}
public SettingDTO GetDTO()
{
SettingDTO dto = new SettingDTO();
dto.Type = this.Type;
dto.Id = this.Id;
dto.Alias = this.Alias;
dto.Description = this.Description;
// type: Bool
dto.BoolValue = this.BoolValue;
// type: Int
dto.IntValue = this.IntValue;
dto.IntMin = this.IntMin;
dto.IntMax = this.IntMax;
dto.IntStep = this.IntStep;
// type: Float
dto.FloatValue = this.FloatValue;
dto.FloatMin = this.FloatMin;
dto.FloatMax = this.FloatMax;
dto.FloatStep = this.FloatStep;
// type: String
dto.StringValue = this.StringValue;
if (this.AllowedValues != null)
{
dto.AllowedValues = new List<string>(this.AllowedValues);
}
// type: Signal
dto.SignalValue = this.SignalValue;
return dto;
}
public static Setting FromDTO(SettingDTO dto)
{
Setting setting = CreateInstance<Setting>();
dto.ApplyTo(setting);
return setting;
}
public enum SettingType
{
Int,
Float,
String,
Bool,
Signal
}
}
[System.Serializable]
public class SettingDTO
{
// general
public Setting.SettingType Type;
public string Id;
public string Alias;
public string Description;
// type: Bool
public bool BoolValue;
// type: Int
public int IntValue;
public int IntMin;
public int IntMax;
public int IntStep;
// type: Float
public float FloatValue;
public float FloatMin;
public float FloatMax;
public float FloatStep;
// type: String
public string StringValue;
public List<string> AllowedValues;
// type: Signal
public string SignalValue;
// Метод для восстановления из DTO в Setting (опционально)
public void ApplyTo(Setting setting)
{
setting.Type = this.Type;
setting.Id = this.Id;
setting.Alias = this.Alias;
setting.Description = this.Description;
setting.BoolValue = this.BoolValue;
setting.IntValue = this.IntValue;
setting.IntMin = this.IntMin;
setting.IntMax = this.IntMax;
setting.IntStep = this.IntStep;
setting.FloatValue = this.FloatValue;
setting.FloatMin = this.FloatMin;
setting.FloatMax = this.FloatMax;
setting.FloatStep = this.FloatStep;
setting.StringValue = this.StringValue;
setting.SignalValue = this.SignalValue;
if (this.AllowedValues != null)
{
setting.AllowedValues = new List<string>(this.AllowedValues);
}
}
// Метод для получения хэша из DTO
public string GetStringForHash()
{
string result = Id;
result += Alias;
result += Description;
switch (Type)
{
case Setting.SettingType.Bool:
break;
case Setting.SettingType.Int:
result += IntMin + IntMax + IntStep;
break;
case Setting.SettingType.Float:
result += FloatMin + FloatMax + FloatStep;
break;
case Setting.SettingType.String:
if (AllowedValues != null)
foreach (string s in AllowedValues) result += s;
break;
case Setting.SettingType.Signal:
result += SignalValue;
break;
}
return result;
}
}