// public override void Awake () // { // #if UNITY_EDITOR // if (!Application.isPlaying) // return; // #endif // base.Awake (); // } public virtual void OnEnable() { #if UNITY_EDITOR if (!Application.isPlaying) { return; } #endif generateRandomGradient = colorGradient2D.xGradients.Length == 0; if (generateRandomGradient) { colorGradient2D = ColorGradient2D.GenerateRandom(colorKeys.x, alphaKeys.x, colorKeys.y, alphaKeys.y); } colorGradient2DRenderer.material.mainTexture = colorGradient2D.MakeSprite(textureSize, pixelsPerUnit).texture; }
public static ColorGradient2D GenerateRandom(int colorBoundaryCount, float distanceMultiplier, FloatRange strengthRange, FloatRange lengthRange, float chanceOfUsingPreviousStrength = 0, float chanceOfUsingPreviousLength = 0, float chanceOfUsingPreviousColor = 0, float chanceOfUsingPreviousPosition = 0, float chanceOfUsingPreviousRotation = 0) { ColorGradient2D output = new ColorGradient2D(); output.distanceMultiplier = distanceMultiplier; float previousStrength = Random.Range(strengthRange.min, strengthRange.max); float previousLength = Random.Range(lengthRange.min, lengthRange.max); Color previousColor = ColorExtensions.RandomColor().SetAlpha(Random.value); Vector2 previousPosition = new Vector2(Random.value, Random.value); Vector2 previousDirection = Random.insideUnitCircle.normalized * previousLength / 2; for (int i = 0; i < colorBoundaryCount; i++) { ColorBoundary colorBoundary = new ColorBoundary(); float strength; if (Random.value < chanceOfUsingPreviousStrength) { strength = previousStrength; } else { strength = Random.Range(strengthRange.min, strengthRange.max); previousStrength = strength; } colorBoundary.strength = strength; float length; if (Random.value < chanceOfUsingPreviousLength) { length = previousLength; } else { length = Random.Range(lengthRange.min, lengthRange.max); previousLength = length; } Color color; if (Random.value < chanceOfUsingPreviousColor) { color = previousColor; } else { color = ColorExtensions.RandomColor().SetAlpha(Random.value); previousColor = color; } colorBoundary.color = color; Vector2 position; if (Random.value < chanceOfUsingPreviousPosition) { position = previousPosition; } else { position = new Vector2(Random.value, Random.value); previousPosition = position; } Vector2 direction; if (Random.value < chanceOfUsingPreviousRotation) { direction = previousDirection; } else { direction = Random.insideUnitCircle.normalized * length / 2; previousDirection = direction; } colorBoundary.lineSegment = new LineSegment2D(position + direction, position - direction); output.colorBoundaries = output.colorBoundaries.Add(colorBoundary); } return(output); }
public static ColorGradient2D GenerateRandom(int xColorKeys, int xAlphaKeys, int yColorKeys, int yAlphaKeys) { ColorGradient2D output = new ColorGradient2D(); Gradient gradient; GradientColorKey colorKey; GradientColorKey[] colorKeys; Gradient previousGradient; int remainderKeyCount = xColorKeys; while (remainderKeyCount >= MAX_GRADIENT_KEYS) { remainderKeyCount -= MAX_GRADIENT_KEYS; } for (int i = 0; i < xColorKeys / MAX_GRADIENT_KEYS; i++) { gradient = new Gradient(); colorKeys = new GradientColorKey[MAX_GRADIENT_KEYS]; for (int i2 = 0; i2 < MAX_GRADIENT_KEYS; i2++) { colorKey = new GradientColorKey(); if (i2 == 0 && output.xGradients.Length > 0) { colorKey.time = 0; previousGradient = output.xGradients[output.xGradients.Length - 1]; colorKey.color = previousGradient.colorKeys[previousGradient.colorKeys.Length - 1].color; } else { if (i2 == MAX_GRADIENT_KEYS - 1) { colorKey.time = 1; } else { colorKey.time = Random.value; } colorKey.color = ColorExtensions.RandomColor(); } colorKeys[i2] = colorKey; } gradient.SetKeys(colorKeys, gradient.alphaKeys); output.xGradients = output.xGradients.Add(gradient); } if (remainderKeyCount > 0) { gradient = new Gradient(); colorKeys = new GradientColorKey[remainderKeyCount]; for (int i = 0; i < remainderKeyCount; i++) { colorKey = new GradientColorKey(); if (i == 0 && output.xGradients.Length > 0) { colorKey.time = 0; previousGradient = output.xGradients[output.xGradients.Length - 1]; colorKey.color = previousGradient.colorKeys[previousGradient.colorKeys.Length - 1].color; } else { if (i == MAX_GRADIENT_KEYS - 1) { colorKey.time = 1; } else { colorKey.time = Random.value; } colorKey.color = ColorExtensions.RandomColor(); } colorKeys[i] = colorKey; } gradient.SetKeys(colorKeys, gradient.alphaKeys); output.xGradients = output.xGradients.Add(gradient); } remainderKeyCount = yColorKeys; while (remainderKeyCount >= MAX_GRADIENT_KEYS) { remainderKeyCount -= MAX_GRADIENT_KEYS; } for (int i = 0; i < yColorKeys / MAX_GRADIENT_KEYS; i++) { gradient = new Gradient(); colorKeys = new GradientColorKey[MAX_GRADIENT_KEYS]; for (int i2 = 0; i2 < MAX_GRADIENT_KEYS; i2++) { colorKey = new GradientColorKey(); if (i2 == 0 && output.yGradients.Length > 0) { colorKey.time = 0; previousGradient = output.yGradients[output.yGradients.Length - 1]; colorKey.color = previousGradient.colorKeys[previousGradient.colorKeys.Length - 1].color; } else { if (i2 == MAX_GRADIENT_KEYS - 1) { colorKey.time = 1; } else { colorKey.time = Random.value; } colorKey.color = ColorExtensions.RandomColor(); } colorKeys[i2] = colorKey; } gradient.SetKeys(colorKeys, gradient.alphaKeys); output.yGradients = output.yGradients.Add(gradient); } if (remainderKeyCount > 0) { gradient = new Gradient(); colorKeys = new GradientColorKey[remainderKeyCount]; for (int i = 0; i < remainderKeyCount; i++) { colorKey = new GradientColorKey(); if (i == 0 && output.yGradients.Length > 0) { colorKey.time = 0; previousGradient = output.yGradients[output.yGradients.Length - 1]; colorKey.color = previousGradient.colorKeys[previousGradient.colorKeys.Length - 1].color; } else { if (i == MAX_GRADIENT_KEYS - 1) { colorKey.time = 1; } else { colorKey.time = Random.value; } colorKey.color = ColorExtensions.RandomColor(); } colorKeys[i] = colorKey; } gradient.SetKeys(colorKeys, gradient.alphaKeys); output.yGradients = output.yGradients.Add(gradient); } GradientAlphaKey alphaKey; GradientAlphaKey[] alphaKeys; int gradientIndex = 0; while (remainderKeyCount >= MAX_GRADIENT_KEYS) { remainderKeyCount -= MAX_GRADIENT_KEYS; } for (int i = 0; i < xAlphaKeys / MAX_GRADIENT_KEYS; i++) { gradient = output.xGradients[gradientIndex]; alphaKeys = new GradientAlphaKey[MAX_GRADIENT_KEYS]; for (int i2 = 0; i2 < MAX_GRADIENT_KEYS; i2++) { alphaKey = new GradientAlphaKey(); if (i2 == 0 && gradientIndex > 0) { alphaKey.time = 0; previousGradient = output.xGradients[gradientIndex - 1]; alphaKey.alpha = previousGradient.alphaKeys[previousGradient.alphaKeys.Length - 1].alpha; } else { if (i2 == MAX_GRADIENT_KEYS - 1) { alphaKey.time = 1; } else { alphaKey.time = Random.value; } alphaKey.alpha = Random.value; } alphaKeys[i2] = alphaKey; } gradient.SetKeys(gradient.colorKeys, alphaKeys); output.xGradients[gradientIndex] = gradient; gradientIndex++; } if (remainderKeyCount > 0) { gradient = output.xGradients[gradientIndex]; alphaKeys = new GradientAlphaKey[remainderKeyCount]; for (int i = 0; i < remainderKeyCount; i++) { alphaKey = new GradientAlphaKey(); if (i == 0 && gradientIndex > 0) { alphaKey.time = 0; previousGradient = output.xGradients[gradientIndex - 1]; alphaKey.alpha = previousGradient.alphaKeys[previousGradient.alphaKeys.Length - 1].alpha; } else { if (i == MAX_GRADIENT_KEYS - 1) { alphaKey.time = 1; } else { alphaKey.time = Random.value; } alphaKey.alpha = Random.value; } alphaKeys[i] = alphaKey; } gradient.SetKeys(gradient.colorKeys, alphaKeys); output.xGradients[gradientIndex] = gradient; gradientIndex++; } gradientIndex = 0; remainderKeyCount = yAlphaKeys; while (remainderKeyCount >= MAX_GRADIENT_KEYS) { remainderKeyCount -= MAX_GRADIENT_KEYS; } for (int i = 0; i < yAlphaKeys / MAX_GRADIENT_KEYS; i++) { gradient = output.yGradients[gradientIndex]; alphaKeys = new GradientAlphaKey[MAX_GRADIENT_KEYS]; for (int i2 = 0; i2 < MAX_GRADIENT_KEYS; i2++) { alphaKey = new GradientAlphaKey(); if (i2 == 0 && gradientIndex > 0) { alphaKey.time = 0; previousGradient = output.yGradients[gradientIndex - 1]; alphaKey.alpha = previousGradient.alphaKeys[previousGradient.alphaKeys.Length - 1].alpha; } else { if (i2 == MAX_GRADIENT_KEYS - 1) { alphaKey.time = 1; } else { alphaKey.time = Random.value; } alphaKey.alpha = Random.value; } alphaKeys[i2] = alphaKey; } gradient.SetKeys(gradient.colorKeys, alphaKeys); output.yGradients[gradientIndex] = gradient; gradientIndex++; } if (remainderKeyCount > 0) { gradient = output.yGradients[gradientIndex]; alphaKeys = new GradientAlphaKey[remainderKeyCount]; for (int i = 0; i < remainderKeyCount; i++) { alphaKey = new GradientAlphaKey(); if (i == 0 && gradientIndex > 0) { alphaKey.time = 0; previousGradient = output.yGradients[gradientIndex - 1]; alphaKey.alpha = previousGradient.alphaKeys[previousGradient.alphaKeys.Length - 1].alpha; } else { if (i == MAX_GRADIENT_KEYS - 1) { alphaKey.time = 1; } else { alphaKey.time = Random.value; } alphaKey.alpha = Random.value; } alphaKeys[i] = alphaKey; } gradient.SetKeys(gradient.colorKeys, alphaKeys); output.yGradients[gradientIndex] = gradient; gradientIndex++; } return(output); }
public virtual void GenerateRandom() { colorGradient = ColorGradient2D.GenerateRandom(colorBoundaryCount, distanceMultiplier, strengthRange, lengthRange, chanceOfUsingPreviousStrength, chanceOfUsingPreviousLength, chanceOfUsingPreviousColor, chanceOfUsingPreviousPosition, chanceOfUsingPreviousRotation); }