Çözüldü Unity her zaman çalışan script

Bu konu çözüldü olarak işaretlenmiştir. Çözülmediğini düşünüyorsanız konuyu rapor edebilirsiniz.

Calimero

Hectopat
Katılım
19 Aralık 2020
Mesajlar
119
Daha fazla  
Cinsiyet
Erkek
Unity 2020.1.0F1 sürümünde image nesnesine gradient uygulayan bir script yazıyorum fakat bu script sadece uygulama çalıştığında renkleri uyguluyor. Onapplicationquit ve awake ile denedim fakat işe yaramadı. Nasıl halledebilirim?

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()
 {
 if (Time.frameCount %60 == 0)
 {

 }
 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:
Çözüm
Unity - Scripting API: ExecuteAlways Unity'nin execute always diye bir özelliği var. Scriptinizin başına eklerseniz hem editörün Update'inde, hem de normal oyunda çalıştırabilirsiniz.

Aynı zamanda Unity - Scripting API: MonoBehaviour.OnValidate() OnValidate de kullanabilirsiniz. Bu da epey fazla yerde çağrılıyor. Örneğin bir objenin üstündeki scriptte değer değiştirdiğinizde (inspector'da) oyunu başlattığınızda bitirdiğinizde vs. Ama dikkat edin, bunu çok fazla yerde kullanmak editör performansınızı ve oyunun başlama süresini hayli düşürecektir.
Unity - Scripting API: ExecuteAlways Unity'nin execute always diye bir özelliği var. Scriptinizin başına eklerseniz hem editörün Update'inde, hem de normal oyunda çalıştırabilirsiniz.

Aynı zamanda Unity - Scripting API: MonoBehaviour.OnValidate() OnValidate de kullanabilirsiniz. Bu da epey fazla yerde çağrılıyor. Örneğin bir objenin üstündeki scriptte değer değiştirdiğinizde (inspector'da) oyunu başlattığınızda bitirdiğinizde vs. Ama dikkat edin, bunu çok fazla yerde kullanmak editör performansınızı ve oyunun başlama süresini hayli düşürecektir.
 
Çözüm
Unity - Scripting API: ExecuteAlways Unity'nin execute always diye bir özelliği var. Scriptinizin başına eklerseniz hem editörün Update'inde, hem de normal oyunda çalıştırabilirsiniz.

Aynı zamanda Unity - Scripting API: MonoBehaviour.OnValidate() onvalidate de kullanabilirsiniz. Bu da epey fazla yerde çağrılıyor. Örneğin bir objenin üstündeki scriptte değer değiştirdiğinizde (inspector'da) oyunu başlattığınızda bitirdiğinizde vs. ama dikkat edin, bunu çok fazla yerde kullanmak editör performansınızı ve oyunun başlama süresini hayli düşürecektir.

Yardımın için teşekkür ederim. ChatGPT ile bir saattir uğraşıyorum fakat yapamamıştım.
 

Geri
Yukarı