I've an issue working with custom editor, my editing focus is changed without any action from my part.
Basically, this is a screen of my custom editor in a neutral state (just added empty translation entity):
And this is a screen when my focus got change without reason. As you can see, my editor detected that 2 keys was the same and notify it by a message at the top of the box, the issue is that I was actually editing the second box's "key" field, and my focus changed on the first box (highlighted blue). If I don't display the warning message, I don't have any issue so I guess it comes from there..
This is my custom editor script :
[CustomEditor(typeof(InternationalizationDatabaseSO))]
public class InternationalizationDatabaseSOEditor : Editor
{
#region Constantes
private const string ITEMS_PROPERTY_NAME = "_mItems";
private const string ITEM_CATEGORY_NAME = "_mCategory";
private const string ITEM_KEY_NAME = "_mKey";
private const string ITEM_VALUES_NAME = "_mValues";
private const string ITEM_VALUE_LANGUAGE = "_mLanguage";
private const string ITEM_VALUE_VALUE = "_mValue";
#endregion
#region Attributs
private InternationalizationDatabaseSO _mDatabase;
#endregion
#region Methods
private void OnEnable()
{
Init();
}
private void Init()
{
_mDatabase = target as InternationalizationDatabaseSO;
}
public override void OnInspectorGUI()
{
serializedObject.Update();
SerializedProperty itemsProperty = serializedObject.FindProperty(ITEMS_PROPERTY_NAME);
int arraySize = itemsProperty.arraySize;
EditorGUI.BeginDisabledGroup(false);
{
EditorGUILayout.BeginHorizontal();
{
GUILayout.FlexibleSpace();
if (OnInspectorGUIButton("+", 40, 25, Color.white, Color.green))
{
itemsProperty.arraySize++;
itemsProperty.GetArrayElementAtIndex(itemsProperty.arraySize - 1).FindPropertyRelative(ITEM_CATEGORY_NAME).stringValue = "";
itemsProperty.GetArrayElementAtIndex(itemsProperty.arraySize - 1).FindPropertyRelative(ITEM_KEY_NAME).stringValue = "";
serializedObject.ApplyModifiedProperties();
Init();
return;
}
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
for(int i = 0; i < arraySize; i++)
{
if(OnInspectorGUIItem(i) == false)
{
serializedObject.ApplyModifiedProperties();
Init();
return;
}
}
}
EditorGUI.EndDisabledGroup();
serializedObject.ApplyModifiedProperties();
}
private bool OnInspectorGUIItem(int index)
{
SerializedProperty itemsProperty = serializedObject.FindProperty(ITEMS_PROPERTY_NAME);
SerializedProperty itemCategory = itemsProperty.GetArrayElementAtIndex(index).FindPropertyRelative(ITEM_CATEGORY_NAME);
SerializedProperty itemKey = itemsProperty.GetArrayElementAtIndex(index).FindPropertyRelative(ITEM_KEY_NAME);
EditorGUI.indentLevel += 1;
EditorGUILayout.BeginVertical(GUI.skin.box);
{
EditorGUILayout.Space();
EditorGUILayout.BeginHorizontal();
{
EditorGUILayout.BeginVertical();
{
if(KeyAlreadyExist(index, itemKey.stringValue))
{
OnInspectorGUIText("Key already exists", 12, Color.red, FontStyle.Bold, false);
}
OnInspectorGUIText("Key : " + itemKey.stringValue, 12, FontStyle.Normal, false);
}
EditorGUILayout.EndVertical();
GUILayout.FlexibleSpace();
if(OnInspectorGUIButton("-", 40, 25, Color.white, Color.red))
{
itemCategory.stringValue = "";
itemKey.stringValue = "";
itemsProperty.DeleteArrayElementAtIndex(index);
return false;
}
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
EditorGUILayout.BeginVertical();
{
GUIStyle style = EditorStyles.foldout;
style.fontSize = 15;
style.fontStyle = FontStyle.Bold;
itemCategory.isExpanded = EditorGUILayout.Foldout(itemCategory.isExpanded, "General informations", style);
if(itemCategory.isExpanded)
{
EditorGUILayout.Space();
EditorGUILayout.PropertyField(itemCategory, new GUIContent("Category"));
EditorGUILayout.PropertyField(itemKey, new GUIContent("Key"));
}
if(OnInspectorGUIItemLanguage(index, itemsProperty.GetArrayElementAtIndex(index)) == false)
{
return false;
}
}
EditorGUILayout.EndVertical();
}
EditorGUILayout.EndVertical();
EditorGUI.indentLevel -= 1;
return true;
}
private bool OnInspectorGUIItemLanguage(int index, SerializedProperty propertyItem)
{
EditorGUILayout.BeginVertical();
{
GUIStyle style = EditorStyles.foldout;
style.fontSize = 15;
style.normal.textColor = Color.black;
style.fontStyle = FontStyle.Bold;
SerializedProperty itemValues = propertyItem.FindPropertyRelative(ITEM_VALUES_NAME);
itemValues.isExpanded = EditorGUILayout.Foldout(itemValues.isExpanded, "Languages informations", style);
if(itemValues.isExpanded)
{
EditorGUILayout.Space();
EditorGUILayout.BeginVertical();
{
EditorGUILayout.BeginHorizontal();
{
int nbTranslation = itemValues.arraySize;
EditorGUILayout.LabelField("Nb translation : " + nbTranslation);
GUILayout.FlexibleSpace();
if (OnInspectorGUIButton("+", 20, 20, Color.white, Color.green))
{
itemValues.arraySize++;
itemValues.GetArrayElementAtIndex(itemValues.arraySize - 1).FindPropertyRelative(ITEM_VALUE_LANGUAGE).intValue = 0;
itemValues.GetArrayElementAtIndex(itemValues.arraySize - 1).FindPropertyRelative(ITEM_VALUE_VALUE).stringValue = "";
return false;
}
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
if (itemValues.arraySize > 0)
{
string translatedLanguages = "{";
foreach (InternationalizationDatabaseItemLanguage item in _mDatabase.Items[index].Values)
{
translatedLanguages += item.Language.ToString() + ", ";
}
if (translatedLanguages.Length > 2)
{
translatedLanguages = translatedLanguages.Remove(translatedLanguages.Length - 2);
}
translatedLanguages += "}";
//EditorStyles.label.stretchHeight = true;
EditorStyles.label.wordWrap = true;
EditorGUILayout.LabelField("Translated : \n" + translatedLanguages);
EditorGUILayout.Space();
for (int i = 0; i < itemValues.arraySize; i++)
{
EditorGUILayout.BeginVertical(GUI.skin.box);
{
EditorGUILayout.Space();
InternationalizationDatabaseItemLanguage item = _mDatabase.Items[index].Values[i];
SerializedProperty propLanguage = itemValues.GetArrayElementAtIndex(i).FindPropertyRelative(ITEM_VALUE_LANGUAGE);
SerializedProperty propValue = itemValues.GetArrayElementAtIndex(i).FindPropertyRelative(ITEM_VALUE_VALUE);
EditorGUILayout.BeginHorizontal();
{
if (LanguageAlreadyExist(index, i, _mDatabase.Items[index].Values[i].Language))
{
OnInspectorGUIText("Warning language translation already exist", 12, Color.red, FontStyle.Bold, false);
}
GUILayout.FlexibleSpace();
if (OnInspectorGUIButton("-", 20, 20, Color.white, Color.red))
{
itemValues.DeleteArrayElementAtIndex(i);
return false;
}
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
EditorGUILayout.PropertyField(propLanguage, new GUIContent("Language"));
EditorGUILayout.PrefixLabel("Translation");
propValue.stringValue = EditorGUILayout.TextArea(propValue.stringValue, GUILayout.Height(80));
}
EditorGUILayout.Space();
EditorGUILayout.EndVertical();
EditorGUILayout.Space();
}
}
}
EditorGUILayout.EndVertical();
}
}
EditorGUILayout.EndVertical();
EditorGUILayout.Space();
return true;
}
private bool KeyAlreadyExist(int currentIndex, string key)
{
for(int i = 0; i < _mDatabase.Items.Count; i++)
{
InternationalizationDatabaseItem item = _mDatabase.Items[i];
if(currentIndex != i && item.Key == key && string.IsNullOrEmpty(key) == false)
{
return true;
}
}
return false;
}
private bool LanguageAlreadyExist(int indexInDatabase, int currentIndex, SystemLanguage language)
{
for (int i = 0; i < _mDatabase.Items[indexInDatabase].Values.Count; i++)
{
InternationalizationDatabaseItemLanguage item = _mDatabase.Items[indexInDatabase].Values[i];
if (currentIndex != i && item.Language == language)
{
return true;
}
}
return false;
}
private void OnInspectorGUIText(string text, FontStyle fontStyle, bool prefixLabel)
{
GUIStyle style = new GUIStyle();
style.fontStyle = fontStyle;
style.wordWrap = true;
if (prefixLabel)
{
EditorGUILayout.PrefixLabel(text, GUIStyle.none, style);
}
else
{
EditorGUILayout.LabelField(text, style);
}
}
private void OnInspectorGUIText(string text, int fontSize, FontStyle fontStyle, bool prefixLabel)
{
GUIStyle style = new GUIStyle();
style.fontSize = fontSize;
style.fontStyle = fontStyle;
style.wordWrap = true;
if (prefixLabel)
{
EditorGUILayout.PrefixLabel(text, GUIStyle.none, style);
}
else
{
EditorGUILayout.LabelField(text, style);
}
}
private void OnInspectorGUIText(string text, int fontSize, Color textColor, FontStyle fontStyle, bool prefixLabel)
{
GUIStyle style = new GUIStyle();
style.fontSize = fontSize;
style.normal.textColor = textColor;
style.fontStyle = fontStyle;
style.wordWrap = true;
if(prefixLabel)
{
EditorGUILayout.PrefixLabel(text, GUIStyle.none, style);
}
else
{
EditorGUILayout.LabelField(text, style);
}
}
private bool OnInspectorGUIButton(string label, int width, int height, Color textColor, Color backgroundColor)
{
Color saveColor = GUI.backgroundColor;
GUILayoutOption[] options = { GUILayout.Width(width), GUILayout.Height(height) };
GUIStyle style = new GUIStyle(GUI.skin.button);
style.normal.textColor = textColor;
GUI.backgroundColor = backgroundColor;
bool pressed = GUILayout.Button(label, style, options);
GUI.backgroundColor = saveColor;
return pressed;
}
#endregion
}
Have you any suggestion ? Thanks
Have a nice day.
Are you sure you are not sharing reference to the same property in both boxes ?
Ok finally figured it out, I mean, I'm not still totally certain why it happens, but found a workaround thanks to : https://forum.unity.com/threads/editor-gui-inputfield-loses-focus-when-gui-updates.542147/
Basically, I'm just displaying always the LabelField, but if there is no error, I simply set the errorMessage to an empty string
My game is a 2D top down platform, I have an zombie enemy that moves up and down the stage, I can place obstacles (such as a create) in its way to stop the enemy, the enemy has the ability to dodge these obstacles at any random moment. If the enemy collided with the create, the enemy will stop its movement to break the create. Once the create is broken, the enemy will continue to moving up, hit point B and then will move back down the stage. My Problem: If the enemy were to dodge left (for example) and then continues to move down and passes another create, the enemy will ignore collision and the function for it to stop and break that create. How do I fix this issue?
public bool hitCollider = false;
public bool hitCollider2 = false;
public bool canMove = true;
public bool moveDown = false;
public bool isMovingUp = true;
public bool isMovingDown = false;
public bool isSomethere = false;
public bool ifMoving;
public float velocidadMax;
public float xMax;
public float xMin;
public float delay;
public float x;
private float tiempo;
public float countDown, countUp, startTime;
void Start () {
Bcol = GetComponent<BoxCollider2D> ();
Bcol.isTrigger = true;
isMovingUp = true;
rigid = GetComponent <Rigidbody2D> ();
velocidadMax = 0.12f;
x = velocidadMax;
InvokeRepeating ("turnFalse", startTime, countDown); // changing lanes
InvokeRepeating ("turnTrue", 0, countUp); // moving up or down
ifMoving = true;
movedownSpeed = -4f;
moveupSpeed = 4f;
hascollidedwHouse = false;
}
void Update () {
if (moveDown == false) {
rigid.velocity = new Vector2 (0, moveupSpeed); // moving up
if (ifMoving == true) {
//irrelevant - don't need to know code here!
} else if (ifMoving == false) {
movedownSpeed = -4;
moveupSpeed = 0;
Move ();
}
}
if (moveDown == true) {
if (hitMascot2 == true) {
} else if (hitMascot2 == false) {
movingDown = true;
Bcol.isTrigger = true;
StartCoroutine (waitMove ());
StartCoroutine (destroyObj ());
}
}
if (isMovingDown == true) {
Debug.Log ("MovingDown");
Bcol.isTrigger = true;
if (canMove == false) {
Debug.Log ("is False" + canMove);
moveupSpeed = 4f;
}
hitCollider = false;
hitCollider2 = false;
}
if (hitCollider == true) {
StartCoroutine (wait ());
Debug.Log ("I HAVE COLLIDED");
if (canMove == true) {
movedownSpeed = 0f;
}
if (canMove == false) {
movedownSpeed = -4f;
}
moveupSpeed = 0f;
}
if (hitCollider2 == true) {
StartCoroutine (wait2 ());
Debug.Log ("I HAVE COLLIDED");
if (canMove == true) {
movedownSpeed = 0f;
}
if (canMove == false) {
movedownSpeed = -4f;
}
moveupSpeed = 0f;
}
}
void OnTriggerEnter2D (Collider2D col) {
if (col.tag == "objects") {
hitCollider = true;
woodBox = true;
canMove = false;
moveDown = false;
xMax = 0;
xMin = 0;
if (hascollidedwHouse == true) {
moveDown = false;
isMovingDown = false;
movedownSpeed = 0;
moveupSpeed = 0;
hitCollider = false;
ifMoving = false;
xMax = 0;
xMin = 0;
StartCoroutine (waitDown ());
}
}
if (col.tag == "objects2") {
hitCollider2 = true;
metalBox = true;
canMove = false;
hitwoodenC = false;
hitmetalC = true;
moveDown = false;
xMax = 0;
xMin = 0;
if (hascollidedwHouse == true) {
moveDown = false;
isMovingDown = false;
movedownSpeed = 0;
moveupSpeed = 0;
ifMoving = false;
hitCollider2 = false;
xMax = 0;
xMin = 0;
StartCoroutine (waitDown2 ());
}
}
if (col.tag == "pointB") {
if (ifMoving == false) {
moveupSpeed = 0;
movedownSpeed = -4;
} else if (ifMoving == true) {
moveupSpeed = 0;
movedownSpeed = -4;
}
Debug.Log ("Collided");
moveDown = true;
isMovingDown = true;
isMovingUp = false;
canMove = false;
hascollidedwHouse = true;
}
void Move () {
if (isSomethere == false) {
if (isMovingUp == true) {
tiempo += Time.deltaTime;
if (transform.localPosition.x < xMin) {
x = Random.Range (0.0f, velocidadMax);
x = velocidadMax;
tiempo = 0.0f;
Debug.Log ("Left");
}
if (tiempo > 1.0f) { //1.0 0.3
x = -velocidadMax;
tiempo = 0.0f;
Debug.Log ("Right");
}
transform.localPosition = new Vector3 (transform.localPosition.x + x, transform.localPosition.y);
}
} else if (isSomethere == true) {
movedownSpeed = -4;
moveupSpeed = 4;
if (iSPdeath == true) {
movedownSpeed = 0;
moveupSpeed = 0;
}
}
}
void turnFalse ()
{
if( hitCollider == false) {
ifMoving = false; // moves left and right
}
if( hitCollider2 == false) {
ifMoving = false; // moves left and right
}
}
void turnTrue() {
if( hitCollider == false) {
ifMoving = true; // moves up and down
}
if( hitCollider2 == false) {
ifMoving = true; // moves up and down
}
}
IEnumerator wait() {
yield return new WaitForSeconds (2.5f);
hitCollider = false;
if (isMovingUp == true) {
canMove = true;
}
if (isMovingUp == false) {
canMove = false;
moveupSpeed = 0f;
movedownSpeed = -4f;
}
if (canMove == true) {
moveupSpeed = 4f;
}
}
IEnumerator wait2() {
yield return new WaitForSeconds (5.2f);
hitCollider = false;
if (isMovingUp == true) {
canMove = true;
}
if (isMovingUp == false) {
canMove = false;
moveupSpeed = 0f;
movedownSpeed = -4f;
}
if (canMove == true) {
moveupSpeed = 4f;
}
}
IEnumerator waitDown() {
yield return new WaitForSeconds (2.5f);
movedownSpeed = -4f;
moveupSpeed = 0f;
moveDown = true;
}
IEnumerator waitDown2() {
yield return new WaitForSeconds (5.2f);
movedownSpeed = -4f;
moveupSpeed = 0f;
moveDown = true;
}
}
I'm fairly new at Unity and i'm trying to making a game.
I want to have an subtitle fading in when you're at the end of the game. This start when you hit a button.
But when I code a image that I fadein, it plays it directly when you start the game.
Do you guys know a solution?
#pragma strict
private var guiShow : boolean = false;
var car : GameObject;
var rayLength = 10;
var guiObject : GUITexture;
var fadeTime = 1.0;
enum Fade {In, Out}
var fadesubtitles : boolean = false;
function Update ()
{
var hit : RaycastHit;
var fwd = transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(transform.position, fwd, hit, rayLength))
{
if(hit.collider.gameObject.tag == "car")
{
guiShow = true;
if(Input.GetKeyDown("e"))
{
guiShow = false;
}
else if(Input.GetKeyDown("e"))
{
guiShow = false;
}
}
}
else
{
guiShow = false;
}
}
function OnGUI()
{
if(guiShow == true)
{
GUI.Box(Rect(Screen.width / 2, Screen.height / 2, 150, 25), "Press F to escape");
if(Input.GetKeyDown("f")){
fadesubtitles = true;
}
}
}
if (fadesubtitles == true){
yield FadeGUITexture(guiObject, fadeTime, Fade.In);
yield WaitForSeconds(3.0);
yield FadeGUITexture(guiObject, fadeTime, Fade.Out);
}
function FadeGUITexture (guiObject : GUITexture, timer : float, fadeType : Fade) {
if (subtitles == true){
var start = fadeType == Fade.In? 0.0 : 1.0;
var end = fadeType == Fade.In? 1.0 : 0.0;
var i = 0.0;
var step = 1.0/timer;
while (i < 1.0) {
i += step * Time.deltaTime;
guiObject.color.a = Mathf.Lerp(start, end, i)*.5;
yield;
}
}
}
I'd start your game object in the 'disabled' state (uncheck it in the inspector). Then at then end of the game, have some code that enables it.
You can use iTween.
FadeFrom(GameObject target, Hashtable args)
Example:
iTween.FadeFrom(gameObject, iTween.Hash("alpha", 0f, "amount", 1f, "time", 2f));
Firstly, this is my script:
#pragma strict
var incorrect: AudioClip;
var correct: AudioClip;
var anything: GameObject;
var diamond: Sprite;
var circle: Sprite;
var triangle: Sprite;
var square: Sprite;
var number: int;
var AcceptInput: boolean = true;
static
var score: int = 1;
var guiScore: GUIText;
function Start() {
number = Random.Range(1, 4);
if (number == 1) {
anything.GetComponent(SpriteRenderer).sprite = diamond;
} else if (number == 2) {
anything.GetComponent(SpriteRenderer).sprite = circle;
} else if (number == 3) {
anything.GetComponent(SpriteRenderer).sprite = triangle;
} else {
anything.GetComponent(SpriteRenderer).sprite = square;
}
}
function Update() {
if (Input.GetMouseButtonDown(0)) {
if (AcceptInput) {
AcceptInput = false;
Debug.Log("Clicked");
if (anything.GetComponent(SpriteRenderer).sprite == diamond) {
audio.PlayOneShot(correct);
guiScore.text = "Score: " + score;
StartCoroutine("YieldTestEnumerator");
AcceptInput = true;
number = Random.Range(1, 4);
if (number == 1) {
anything.GetComponent(SpriteRenderer).sprite = diamond;
} else if (number == 2) {
anything.GetComponent(SpriteRenderer).sprite = circle;
} else if (number == 3) {
anything.GetComponent(SpriteRenderer).sprite = triangle;
} else {
anything.GetComponent(SpriteRenderer).sprite = square;
}
} else if (anything.GetComponent(SpriteRenderer).sprite == circle) {
Debug.Log("Wrong Answer!");
Application.LoadLevel("GameOver");
audio.PlayOneShot(incorrect);
} else if (anything.GetComponent(SpriteRenderer).sprite == triangle) {
Debug.Log("Wrong Answer!");
Application.LoadLevel("GameOver");
audio.PlayOneShot(incorrect);
} else if (anything.GetComponent(SpriteRenderer).sprite == square) {
Debug.Log("Wrong Answer!");
Application.LoadLevel("GameOver");
audio.PlayOneShot(incorrect);
}
}
}
}
function YieldTestEnumerator() {
yield WaitForSeconds(0.5);
}
As you can see it is a multiple choice game. For some stupid and weird reason, if I press ANYWHERE on the screen, it would just load the "GameOver" scene, even though I got the correct answer. Why does it detect a touch from ANYWHERE on the screen? Thats just so weird. Does any of yous have any ideas how to fix this? I don't get this. Any help would be appreciated.
Attach a collider 2D to your anything and work with
OnMouseDown()
{
var lSprite : Sprite ;
lSprite = GetComponent(SpriteRenderer).sprite ;
if (lSprite == diamond) {
audio.PlayOneShot(correct);
guiScore.text = "Score: " + score;
StartCoroutine("YieldTestEnumerator");
AcceptInput = true;
number = Random.Range(1, 4);
if (number == 1) {
lSprite = diamond;
} else if (number == 2) {
lSprite = circle;
} else if (number == 3) {
lSprite = triangle;
} else {
lSprite = square;
}
} else
if (lSprite == circle) {
Debug.Log("Wrong Answer!");
Application.LoadLevel("GameOver");
audio.PlayOneShot(incorrect);
} else
if (lSprite == triangle) {
Debug.Log("Wrong Answer!");
Application.LoadLevel("GameOver");
audio.PlayOneShot(incorrect);
} else
if (lSprite == square) {
Debug.Log("Wrong Answer!");
Application.LoadLevel("GameOver");
audio.PlayOneShot(incorrect);
}
}
Currently building a 2D game, and have created a javascript code for the jump for my character 'Ezio' but it does not do anything when i press 'space' for it to jump. There are no errors with the code either.
#pragma strict
var jump :float = 0;
var jumpspeed : float = 15;
var jumptimer :float = 0;
function Start () {
}
function Update () {
if (jump == 1) {
jumptimer = jumptimer +1;
if (jumptimer >= 50) {
jumptimer = 0;
jump = 0;
}
}
}
if (Input.GetKeyDown ("space"))
{
if (jump == 0) {
rigidbody2D.velocity.y = jumpspeed;
jump = 1;
}
}
Any suggestions on what could be the issue?
Try this:
#pragma strict
var jump :float = 0;
var jumpspeed : float = 15;
var jumptimer :float = 0;
function Start () {
}
function Update() {
if (Input.GetKeyDown("space")) {
if (jump == 1) {
jumptimer = jumptimer + 1;
if (jumptimer >= 50) {
jumptimer = 0;
jump = 0;
}
} else {
rigidbody2D.velocity.y = jumpspeed;
jump = 1;
}
}
}