C# Unity Always Editable Script nasıl yapılır?

Calimero

Hectopat
Katılım
19 Aralık 2020
Mesajlar
120
Daha fazla  
Cinsiyet
Erkek
Scriptimde belirli değerleri PlayerPrefs ile kaydediyorum fakat kullanıcının bu değerleri tekrardan görmesi için uygulamayı başlatması gerek. Nasıl Scriptimi Sürekli Düzeltilebilir yapabilirim.
Kodlarım:

C#:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using static UnityEditor.Progress;

public class ColorChangerScript : MonoBehaviour
{
 private Color Color1; // Başlangıç rengi
 private Color Color2; // Bitiş rengi

 private Image imageComponent;

 private bool swapColors = false;

 private Option Style;
 private float mixPosition;

 private string mixPositionKey = "MixPosition";
 private string styleKey = "Style";

 public enum Option
 {
 Horizontal,
 Vertical,
 Cross
 }

 void Start()
 {
 // GameObject üzerindeki Image bileşenini al
 imageComponent = GetComponent<Image>();
 LoadSettings();
 ApplyGradient();
 }

 private void Update()
 {
 ApplyGradient();
 }

 void ApplyGradient()
 {
 if (swapColors)
 {
 // Renkleri değiştir
 Color tempColor = Color1;
 Color1 = Color2;
 Color2 = tempColor;

 // Bool değişkeni sıfırla
 swapColors = false;
 }

 switch (Style)
 {
 case Option.Horizontal:
 GradientYap();
 break;
 case Option.Cross:
 GradientYapCapraz();
 break;
 case Option.Vertical:
 GradientYapDikey();
 break;
 }

 SaveSettings();
 }

 void GradientYap()
 {
 int textureWidth = 256;
 int textureHeight = 1;

 // Gradient texture oluştur
 Texture2D gradientTexture = GradientTexture(Color1, Color2, textureWidth, mixPosition);

 // Image bileşenine texture'ı uygula
 imageComponent.sprite = Sprite.Create(gradientTexture, new Rect(0, 0, textureWidth, textureHeight), new Vector2(0.5f, 0.5f));
 }

 void GradientYapDikey()
 {
 int textureWidth = 1;
 int textureHeight = 256;

 Texture2D gradientTexture = GradientTextureDikey(Color1, Color2, textureHeight, mixPosition);

 imageComponent.sprite = Sprite.Create(gradientTexture, new Rect(0, 0, textureWidth, textureHeight), new Vector2(0.5f, 0.5f));
 }

 void GradientYapCapraz()
 {
 int textureWidth = 256;
 int textureHeight = 256;

 Texture2D gradientTexture = GradientTextureCapraz(Color1, Color2, textureWidth, textureHeight, mixPosition);

 // Image bileşenine texture atanır.
 imageComponent.sprite = Sprite.Create(gradientTexture, new Rect(0, 0, textureWidth, textureHeight), new Vector2(0.5f, 0.5f));
 }

 Texture2D GradientTexture(Color startColor, Color endColor, int textureWidth, float mixPosition)
 {
 Texture2D texture = new Texture2D(textureWidth, 1);
 Color[] gradientColors = new Color[textureWidth];

 for (int i = 0; i < textureWidth; i++)
 {
 float t = Mathf.InverseLerp(0, textureWidth - 1, i);
 gradientColors[i] = Color.Lerp(startColor, endColor, Mathf.Lerp(0, 1, Mathf.Clamp01(t - mixPosition)));
 }

 texture.SetPixels(gradientColors);
 texture.Apply();

 return texture;
 }
 Texture2D GradientTextureDikey(Color startColor, Color endColor, int textureHeight, float mixPosition)
 {
 Texture2D texture = new Texture2D(1, textureHeight);
 Color[] gradientColor = new Color[textureHeight];

 for (int i = 0; i < textureHeight; i++)
 {
 float t = Mathf.InverseLerp(0, textureHeight - 1, i);
 gradientColor[i] = Color.Lerp(startColor, endColor, Mathf.Lerp(0, 1, Mathf.Clamp01(t - mixPosition)));
 }

 texture.SetPixels(gradientColor);
 texture.Apply();

 return texture;
 }

 Texture2D GradientTextureCapraz(Color startColor, Color endColor, int textureWidth, int textureHeight, float mixPosition)
 {
 Texture2D texture = new Texture2D(textureWidth, textureHeight);
 Color[] gradientColors = new Color[textureWidth * textureHeight];

 for (int y = 0; y < textureHeight; y++)
 {
 for (int x = 0; x < textureWidth; x++)
 {
 float t = Mathf.InverseLerp(0, textureWidth - 1, x);
 float u = Mathf.InverseLerp(0, textureHeight - 1, y);

 Color lerpedColor = Color.Lerp(startColor, endColor, Mathf.Lerp(0, 1, Mathf.Clamp01(t + u - mixPosition)));
 gradientColors[y * textureWidth + x] = lerpedColor;
 }
 }

 texture.SetPixels(gradientColors);
 texture.Apply();

 return texture;
 }

 void SaveSettings()
 {
 PlayerPrefs.SetFloat("color1RKey", Color1.r);
 PlayerPrefs.SetFloat("color1GKey", Color1.g);
 PlayerPrefs.SetFloat("color1BKey", Color1.b);
 PlayerPrefs.SetFloat("color1AKey", Color1.a);

 PlayerPrefs.SetFloat("color2RKey", Color2.r);
 PlayerPrefs.SetFloat("color2GKey", Color2.g);
 PlayerPrefs.SetFloat("color2BKey", Color2.b);
 PlayerPrefs.SetFloat("color2AKey", Color2.a);

 PlayerPrefs.SetFloat(mixPositionKey, mixPosition);
 PlayerPrefs.SetInt(styleKey, (int)Style);

 PlayerPrefs.Save();
 }

 void LoadSettings()
 {
 Color1 = new Color(
 PlayerPrefs.GetFloat("color1RKey", Color1.r),
 PlayerPrefs.GetFloat("color1GKey", Color1.g),
 PlayerPrefs.GetFloat("color1BKey", Color1.b),
 PlayerPrefs.GetFloat("color1AKey", Color1.a)
 );

 Color2 = new Color(
 PlayerPrefs.GetFloat("color2RKey", Color2.r),
 PlayerPrefs.GetFloat("color2GKey", Color2.g),
 PlayerPrefs.GetFloat("color2BKey", Color2.b),
 PlayerPrefs.GetFloat("color2AKey", Color2.a)
 );

 mixPosition = PlayerPrefs.GetFloat(mixPositionKey, mixPosition);
 Style = (Option)PlayerPrefs.GetInt(styleKey, (int)Style);
 }

 Color GetColorFromString(string colorString)
 {
 Color color;
 ColorUtility.TryParseHtmlString(colorString, out color);
 return color;
 }

 [CustomEditor(typeof(ColorChangerScript))]
 public class ColorChangerScriptEditor : Editor
 {
 override public void OnInspectorGUI()
 {
 var myScript = target as ColorChangerScript;
 using (var group = new EditorGUILayout.FadeGroupScope(Convert.ToSingle(true)))
 {
 EditorGUI.indentLevel++;
 myScript.Color1 = EditorGUILayout.ColorField("Color 1", myScript.Color1);
 myScript.Color2 = EditorGUILayout.ColorField("Color 2", myScript.Color2);

 if (myScript.Style == Option.Cross)
 {
 myScript.mixPosition = EditorGUILayout.Slider("Number", myScript.mixPosition, -1f, 2f);
 }
 else
 {
 myScript.mixPosition = EditorGUILayout.Slider("Number", myScript.mixPosition, -1f, 1f);
 }

 myScript.Style = (Option)EditorGUILayout.EnumPopup("Option", myScript.Style);

 // Renkleri değiştirme butonu
 if (GUILayout.Button("Swap Colors"))
 {
 myScript.swapColors = true;
 }

 EditorGUI.indentLevel--;
 }
 }
 }
}
 
Son düzenleyen: Moderatör:
Bunu playerprefs yerine bir json dosyasına kaydedip bu dosyaya dışarıdan müdahale edilmesine izin verseniz nasıl olur? Playerprefs de aslında bir yerde txt dosyasına kaydediyor. Bunun yerine bir json dosyası oluşturup değiştirmek bu işi çözmez mi?

Sizin kodunuzda bunu nasıl yapabileceğinize dair bir şey yaptım. Tabi bunu yaparken mevcut dosya konumunda dosyanın olduğunu vs kontrol etmek gerek.

Öncelikle json formatında yazmak için class'ımızı oluşturalım.

C#:
[System.Serializable]
public class SettingsData
{
    public float color1R;
    public float color1G;
    public float color1B;
    public float color1A;

    public float color2R;
    public float color2G;
    public float color2B;
    public float color2A;

    public float mixPosition;
    public int style;
}

ardından save ve load fonksiyonlarını güncelleyelim.


C#:
void SaveSettings()
{
    SettingsData data = new SettingsData
    {
        color1R = Color1.r,
        color1G = Color1.g,
        color1B = Color1.b,
        color1A = Color1.a,

        color2R = Color2.r,
        color2G = Color2.g,
        color2B = Color2.b,
        color2A = Color2.a,

        mixPosition = mixPosition,
        style = (int)Style
    };

    string json = JsonUtility.ToJson(data);
    File.WriteAllText(Application.persistentDataPath + "/settings.json", json);
}

void LoadSettings()
{
    string path = Application.persistentDataPath + "/settings.json";
    if (File.Exists(path))
    {
        string json = File.ReadAllText(path);
        SettingsData data = JsonUtility.FromJson<SettingsData>(json);

        Color1 = new Color(data.color1R, data.color1G, data.color1B, data.color1A);
        Color2 = new Color(data.color2R, data.color2G, data.color2B, data.color2A);
        mixPosition = data.mixPosition;
        Style = (Option)data.style;
    }
}
 
Son düzenleme:

Technopat Haberler

Geri
Yukarı