So I’ve been struggling for a few hours now, with a strange thing, so let me explain you the problem.
I have two scripts. One script have one method that I want to call it in the edit mode, so I built the EditorScript and added the Run button which calls the function. In edit mode everything works fine, but when I run the game, some of the modifications that the script did changed back.
Here is a photo with my problem
In the first picture I have the GameObject before running the script
In the second picture is what I get after running the script
In the third is what I get after playing the game.
Here is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using PixelCrushers.DialogueSystem;
public class ActionButton : MonoBehaviour {
public Sprite buttonSprite;
private Sprite emptySprite;
public void Run(){
emptySprite = Resources.Load<Sprite>("Empty");
string tName = buttonSprite.name;
string sufix = tName.Remove(0,tName.IndexOf("_!_")+3);
int sufixSepIndex = sufix.LastIndexOf('_');
int widthStart = sufix.IndexOf('w')+1;
int widthEnd = sufix.IndexOf('h',widthStart);
float width = float.Parse(sufix.Substring(widthStart, widthEnd-widthStart));
print(width);
int heightStart = sufix.IndexOf('h')+1;
int heightEnd = sufix.IndexOf('_',heightStart);
float height = float.Parse(sufix.Substring(heightStart,heightEnd-heightStart));
print(height);
int xStart = sufix.IndexOf('x')+1;
int xEnd = sufix.IndexOf('y',xStart);
float x = float.Parse(sufix.Substring(xStart,xEnd - xStart));
int yStart = sufix.IndexOf('y')+1;
float y = float.Parse(sufix.Substring(yStart));
print(string.Concat("width :",width,"___ height :",height, "\nx :", x, "___ y :", y));
//position
RectTransform rt = gameObject.GetComponent<RectTransform>();
rt.anchoredPosition = new Vector3(x,-y,0);
rt.anchorMin = new Vector2(0, 1);
rt.anchorMax = new Vector2(0, 1);
rt.sizeDelta = new Vector2(width,height);
//change image
gameObject.GetComponent<Image>().sprite = emptySprite;
//set button
Button btn = gameObject.GetComponent<Button>();
btn.transition = Selectable.Transition.SpriteSwap;
SpriteState ss = new SpriteState();
ss.highlightedSprite = buttonSprite;
ss.pressedSprite = buttonSprite;
btn.spriteState = ss;
//change name
string conversationName = gameObject.GetComponent<ConversationTrigger>().conversation;
gameObject.name = conversationName;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(ActionButton))]
public class ActionButtonEditor : Editor {
public override void OnInspectorGUI(){
DrawDefaultInspector();
ActionButton actionButton = (ActionButton)target;
GUILayout.Space(10);
if(GUILayout.Button("Run")){
actionButton.Run();
}
}
}
Thank you!
