I'm a Unity tools developer and i want to put a GUILayout.Label(" title ") inside a FoldoutHeaderGroup so i did that :
using UnityEditor;
using UnityEngine;
public class tesdtEditor : EditorWindow
{
private bool showWindowFoldOut;
[MenuItem("test")]
public static void ShowWindow()
{
EditorWindow.GetWindow(typeof(tesdtEditor));
}
public void OnGUI()
{
showWindowFoldOut = EditorGUILayout.BeginFoldoutHeaderGroup(showWindowFoldOut, "foldout Name");
GUILayout.Label("title");
EditorGUILayout.EndFoldoutHeaderGroup();
}
}
But it's not in my FoldoutHeaderGroup (Screen of the window)
I can't see where I'm mistaken, can someone guide me through ?
You still need to check the
if(showWindowFoldOut)
{
EditorGUI.indentLevel++;
GUILayout.Label("title");
EditorGUI.indentLevel--;
}
See example in EditorGUILayout.BeginFoldoutHeaderGroup.
Tbh besides the different styling I didn't understand so far what the difference is between that and a simple EditorGUILayout.Foldout.
Related
I tried to ask user for permission for read memory on Android device but nothing works. Then I tried to check internal code of Unity (Permission struct in UnityEngine.Android), and there was empty function. The version of my unity 2020.3.15f2. Then I tried to use earlier version of Unity but there the Permission struct was different. It's my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Android;
public class Perm : MonoBehaviour
{
readonly string[] _permissions = {Permission.ExternalStorageRead, Permission.ExternalStorageWrite};
// Start is called before the first frame update
void Awake()
{
#if UNITY_ANDROID
Permission.RequestUserPermission(Permission.ExternalStorageRead);
#endif
RequestPermission();
StartCoroutine(FckIt());
}
public void RequestPermission()
{
if (!Permission.HasUserAuthorizedPermission(Permission.ExternalStorageRead))
Permission.RequestUserPermissions(_permissions);
}
IEnumerator FckIt()
{
yield return new WaitForSeconds(10f);
RequestPermission();
}
}
There are a lot of repetitions here - I tried to fix the error myself but it didn't work out.
I have had problems with the OnDrop method, it is that it is not called, I was reading and maybe it has something to do with a Raycaster component, but I'm not sure, I don't even have knowledge of it, if someone could explain this to me I would greatly appreciate it.
Here is my code in c # plus an image of my hierarchy in Unity2D:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Drop : MonoBehaviour, IDropHandler
{
public void OnDrop(PointerEventData eventData)
{
if (eventData.pointerDrag != null)
{
Debug.Log("Aqui esta");
}
}
}
From already thank you very much.
OnDrop method is not called
To ensure the Method gets called you need to ensure that both the name and inherited class you use is correct.
As it seems you need to use override for each function and instead of IDropHandler and MonoBehaviour inherit from EventTrigger.
Example:
using UnityEngine;
using UnityEngine.EventSystems;
public class Drop : EventTrigger {
public override void OnDrop(PointerEventData eventData) {
// Check if the eventData is Null.
if (eventData.pointerDrag != null) {
Debug.Log("OnDrop called.");
}
}
}
EventTrigger
I have tried using
using UnityEngine;
using UnityEngine.SceneManagement;
public class LoadBScene : MonoBehaviour
{
void Start()
{
Debug.Log("LoadSceneB");
}
public void LoadB(string SceneB)
{
Debug.Log("sceneName to load: " + SceneB);
SceneManager.LoadScene(SceneB);
}
}
I have not been able to get the scene to change.
Anyone have any ideas?
Please and thank you!
Looks to me like the problem here is that you simply aren't calling the LoadB() method which means that SceneManager.LoadScene() is never reached. Inside the Start method just put
LoadB("InsertSceneNameHere")
I'm trying to sort an array in Unity by name, using Array.Sort().
I've been reading as much as I can but still can't adapt it into my little project here. Here is what I have so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class UIController : MonoBehaviour, IComparable<Slot>
{
public static UIController instance;
public Text uiMessageBox;
public Slot[] slots;
private void Awake()
{
if (instance == null)
instance = this;
else
Destroy(this);
DontDestroyOnLoad(this);
slots = FindObjectsOfType<Slot>();
Array.Sort(slots, ); // HELP: NOT SURE WHAT TO PUT HERE
}
public int CompareTo(Slot other)
{
return this.name.CompareTo(other.name);
}
}
Note, I deleted the parts I think are irrelevant in this class (such as the code that displays a message string on screen etc).
ALSO NOTE: I implement here IComparable<Slot> but I also tried it with IComparable<UIController>. (like I say, I've seen lots of examples here and other websites, but cannot quite get it to work in my code.)
Why not use delegate form?
Array.Sort(slots, (slot1, slot2) => slot1.name.CompareTo(slot2.name));
If you still want to implement the IComparable interface, you must write it inside the Slot class.
And you can also implement IComparer interface in any class.
class AnyClass : IComparer<Slot>
{
public int Compare(Slot slot1, Slot slot2)
{
return slot1.name.CompareTo(slot2.name);
}
}
I was able to keep the code within my UIController class which is how I imagined it would be (since I built the array of slots there , it felt right for me to sort it there also.)
Here's how its done:
public class UIController : MonoBehaviour, IComparer<Slot>
{
public static UIController instance;
public Text uiMessageBox;
public Slot[] slots;
private void Awake()
{
slots = FindObjectsOfType<Slot>();
Array.Sort(slots, this); // i just passed 'this' as the IComparer parameter :)
}
public int Compare(Slot x, Slot y)
{
return x.name.CompareTo(y.name);
}
}
I wrote the following 2 scripts in Unity 3D , PhysicsObject and PlayerPlatformerController (following this tutorial). The PlayerPlatformerController script is attached to a game object.
PhysicsObject.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PhysicsObject : MonoBehaviour {
void Update () {
ComputeVelocity ();
}
protected virtual void ComputeVelocity() {
}
}
PlayerPlatformerController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerPlatformerController : PhysicsObject {
void Update () {
}
protected override void ComputeVelocity() {
}
}
The codes look simple, but the ComputeVelocity() in PlayerPlatformerController does not get called (proved by adding Debug.Log()). Why?
If I change to the following codes, the function works perfectly:
PhysicsObject.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PhysicsObject : MonoBehaviour {
void Update () {
//ComputeVelocity ();
}
/*protected virtual void ComputeVelocity() {
}*/
}
PlayerPlatformerController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerPlatformerController : PhysicsObject {
void Update() {
ComputeVelocity();
}
void ComputeVelocity() {
}
}
What did I miss?
It happened to me that I had two scripts:
Health.cs
HealthModified.cs -> This was overriding function of Health.cs
In my GameObject I was testing HealthModified (enabled) and I also had the Health.cs in the gameobject but disabled.
The code was using the Halth.cs compoenent also when it was disabled, I had to remove the component from the gameobject to get the HealthModified behaviour.
It may happen because you use Update method in child-class (PlayerPlatformerController). If it true, you will mark update as protected and virtual, and override it in child class. It may look like this:
public class PhysicsObject : MonoBehaviour
{
protected virtual void Update()
{
ComputeVelocity();
Debug.LogFormat("PhysicsObject Update");
}
protected virtual void ComputeVelocity()
{
Debug.LogFormat("PhysicsObject ComputeVelocity");
}
}
public class PlayerPlatformerController : PhysicsObject
{
protected override void Update()
{
base.Update();
Debug.LogFormat("PlayerPlatformerController Update");
}
protected override void ComputeVelocity()
{
base.ComputeVelocity();
Debug.LogFormat("PlayerPlatformerController ComputeVelocity");
}
}
I finally found out the problem. In the PlayerPlatformerController class, if I remove the Update() method, the override ComputeVelocity() will start working.
Thanks all for your efforts helping me.
When you have a class that inherits from another you need to be wary of accidentally overriding the parent's methods.
In this case, creating empty Start() and Update() methods will replace those from the parent. This is an easy mistake to make since new classes are normally created with those methods by default.
Removing the methods from the child class will ensure the parent is called instead. Or if you want to keep the child methods while also calling the parent's, you can use base.Start() (or whichever method you are overriding).