备份CatanBuilding瘦身独立工程
This commit is contained in:
144
Assets/Scripts/AOTScripts/QualityAdapter.cs
Normal file
144
Assets/Scripts/AOTScripts/QualityAdapter.cs
Normal file
@@ -0,0 +1,144 @@
|
||||
using asap.core;
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UniRx;
|
||||
|
||||
public struct QualityChangedEvt { }
|
||||
|
||||
public class QualityManager
|
||||
{
|
||||
private const string Quality_Level = "QualityLevel";
|
||||
public static void ChangeQuality(int level)
|
||||
{
|
||||
string[] names = QualitySettings.names;
|
||||
if (level >= names.Length)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
QualitySettings.SetQualityLevel(level);
|
||||
PlayerPrefs.SetInt(Quality_Level, level);
|
||||
GContext.Publish(new QualityChangedEvt());
|
||||
}
|
||||
|
||||
public static void ChangeQuality(string levelName)
|
||||
{
|
||||
var names = QualitySettings.names;
|
||||
for (int i = 0; i < names.Length; i++)
|
||||
{
|
||||
if (names[i] == levelName)
|
||||
{
|
||||
ChangeQuality(i);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void AutoSetQualityLevel()
|
||||
{
|
||||
var level=PlayerPrefs.GetInt(Quality_Level, -1);
|
||||
if (level == -1)
|
||||
{
|
||||
level = GpuEvaluation.Tier;
|
||||
}
|
||||
ChangeQuality(level);
|
||||
}
|
||||
|
||||
public static string[] QualityNames => QualitySettings.names;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class QualityAdapter : MonoBehaviour
|
||||
{
|
||||
[Serializable]
|
||||
private class AdapterData
|
||||
{
|
||||
public GameObject[] enableObjects;
|
||||
public GameObject[] disableObjects;
|
||||
public Renderer renderer;
|
||||
public string[] keywordsToEnable;
|
||||
public string[] keywordsToDisable;
|
||||
public string[] propToEnable;
|
||||
public string[] propToDisable;
|
||||
}
|
||||
|
||||
[SerializeField]
|
||||
private AdapterData[] adapterDatas;
|
||||
|
||||
private IDisposable disposable = null;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
disposable = GContext.OnEvent<QualityChangedEvt>().Subscribe(_ => SetQualityLevel());
|
||||
SetQualityLevel();
|
||||
}
|
||||
|
||||
private void SetQualityLevel()
|
||||
{
|
||||
int level = QualitySettings.GetQualityLevel();
|
||||
if (level >= adapterDatas.Length)
|
||||
{
|
||||
level = adapterDatas.Length - 1;
|
||||
}
|
||||
var adapterData = adapterDatas[level];
|
||||
|
||||
if (adapterData.enableObjects != null)
|
||||
{
|
||||
foreach (var go in adapterData.enableObjects)
|
||||
{
|
||||
go.SetActive(true);
|
||||
}
|
||||
}
|
||||
|
||||
if (adapterData.disableObjects != null)
|
||||
{
|
||||
foreach (var go in adapterData.disableObjects)
|
||||
{
|
||||
go.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
if (adapterData.renderer != null)
|
||||
{
|
||||
var mat = adapterData.renderer.material;
|
||||
var keywordsToEnable = adapterData.keywordsToEnable;
|
||||
if (keywordsToEnable != null)
|
||||
{
|
||||
foreach (var keyword in keywordsToEnable)
|
||||
{
|
||||
mat.EnableKeyword(keyword);
|
||||
}
|
||||
}
|
||||
var keywordsToDisable = adapterData.keywordsToDisable;
|
||||
if (keywordsToDisable != null)
|
||||
{
|
||||
foreach (var keyword in keywordsToDisable)
|
||||
{
|
||||
mat.DisableKeyword(keyword);
|
||||
}
|
||||
}
|
||||
var propToEnable = adapterData.propToEnable;
|
||||
if (propToEnable != null)
|
||||
{
|
||||
foreach (var prop in propToEnable)
|
||||
{
|
||||
mat.SetInt(prop, 1);
|
||||
}
|
||||
}
|
||||
var propToDisable = adapterData.propToDisable;
|
||||
if (propToDisable != null)
|
||||
{
|
||||
foreach (var prop in propToDisable)
|
||||
{
|
||||
mat.SetInt(prop, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
disposable?.Dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user