- Home /
Is it possible to write a function in the backend of an editor script for a scriptableObject?
I am working on some autonomous functionality to make life easier to work on a game I prototypes back in 2017.
The functionality is to essentially splice up a sprite and behind the scenes add it to an array of Sprite sprites[].
I am wondering how can I make an editor script, access and use variables from the other script and then create an array of sprites in which is on the ScriptableObject?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(SetOfPrimatives))]
public class PrimativeEditor : Editor
{
SerializedProperty TagType;
SerializedProperty SpriteSheet;
SerializedProperty GridSize;
SerializedProperty PixelSpacing;
SerializedProperty sprites;
public int test = 0;
SetOfPrimatives Primative;
//SetOfPrimatives
private void OnEnable()
{
TagType = serializedObject.FindProperty("TagType");
SpriteSheet = serializedObject.FindProperty("SpriteSheet");
sprites = serializedObject.FindProperty("sprites");
GridSize = serializedObject.FindProperty("GridSize");
PixelSpacing = serializedObject.FindProperty("PixelSpacing");
// Primative = ((ScriptableObject)target)gameObject.GetComponent<SetOfPrimatives>();
}
public override void OnInspectorGUI()
{
serializedObject.Update();
if (GUILayout.Button("Run Function"))
{
SpriteCropping();
}
EditorGUILayout.PropertyField(TagType);
EditorGUILayout.PropertyField(SpriteSheet);
EditorGUILayout.IntField(test);
serializedObject.ApplyModifiedProperties();
}
Rect Origin;
int GridWidth;
int GridHeight;
public void SpriteCropping()
{
if (Primative != null)
{
if (SpriteSheet != null)
{
Origin = Primative.SpriteSheet.rect;
}
Vector2 position;
int i = Mathf.RoundToInt(Origin.width / (Primative.GridSize.x + Primative.PixelSpacing)) * Mathf.RoundToInt(Origin.height / (Primative.GridSize.y + Primative.PixelSpacing));
GridWidth = Mathf.RoundToInt(Origin.width / (Primative.GridSize.x + Primative.PixelSpacing));
GridHeight = Mathf.RoundToInt(Origin.height / (Primative.GridSize.y + Primative.PixelSpacing));
Primative.sprites = new Sprite[i];
int ice = 0;
for (int a = GridHeight - 1; a > -1; a--)
{
for (int aa = 0; aa < GridWidth; aa++)
{
position.x = (Primative.GridSize.x + Primative.PixelSpacing) * aa;
position.y = (Primative.GridSize.y + Primative.PixelSpacing) * a;
Debug.Log((position.x + Primative.GridSize.x) + ", " + (position.y + Primative.GridSize.y));
if (position.x + Primative.GridSize.x <= Origin.width && position.y + Primative.GridSize.y <= Origin.height) //Safety first!
{
Primative.sprites[ice] = Sprite.Create(Primative.SpriteSheet.texture, new Rect(position.x, position.y, Primative.GridSize.x, Primative.GridSize.y), new Vector2(0, 0));
ice++;
}
}
}
}
}
}
Encase it's a little confusing. I created a script that works in Playmode. I could also make it work in Editormode, but I want it to work when I create a new Object of Primitive which derives from ScriptableObject. Then I add a Sprite Sheet to that Object of which calls the function if and when it changes and is not equal to null.
Or from an EditorScript, is it possible to directly influence variables through code and get those Variables, make changes and send them back once?
In theory, so long as you have all relevant variables and references in the original script the editor will be applied to, you can directly access the values of their properties and manipulate them.
Your answer
Follow this Question
Related Questions
Confused about custom GameObjects,Custom GameObject confusion 0 Answers
There is no GameObject attached to this GameObject 2 Answers
How to unfreeze a Script in a unity5 Scene? 1 Answer
How do I associate my custom object for the serializedProperty objectReferenceValue 1 Answer
Custom Grid Object Always Starts at 0,0 of editor window. 0 Answers