Search Unity

Question 1 Script 2 Classes (Editor & Mono) Help

Discussion in 'Editor & General Support' started by renman3000, Mar 6, 2023.

  1. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Hi there,
    I found this very helpful third-party asset, from YouTube. The issue is I can not add it to a game object. If you see the code, the script has within it, an Editor Class and a MonoBehavior Class.

    So, I am not sure how to insure the script can be applied.

    Any tips? Naming Convecniton? Separate the Classes into two scripts?

    Thanks
    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.Threading;
    6. using UnityEditor;
    7. using UnityEngine;
    8.  
    9. [CustomEditor(typeof(IKSolver)), CanEditMultipleObjects]
    10. public class E_IKSolver : Editor
    11. {
    12.     public override void OnInspectorGUI()
    13.     {
    14.         IKSolver solver = (IKSolver)target;
    15.         if (solver.needResetOption)
    16.         {
    17.             GUI.enabled = false;
    18.         }
    19.         DrawDefaultInspector();
    20.         if (solver.needResetOption)
    21.         {
    22.             GUI.enabled = true;
    23.             if (GUILayout.Button("Reset Scene Hierarchy"))
    24.             {
    25.                 solver.ResetHierarchy();
    26.             }
    27.         }
    28.     }
    29. }
    30.  
    31. [ExecuteInEditMode]
    32. public class IKSolver : MonoBehaviour
    33. {
    34.     [Serializable]
    35.     public class Bone
    36.     {
    37.         public Transform bone;
    38.         [HideInInspector]
    39.         public float length;
    40.         [HideInInspector]
    41.         public Vector3 origPos, origScale;
    42.         [HideInInspector]
    43.         public Quaternion origRot;
    44.     }
    45.  
    46.     [Header("Bones - Leaf to Root")]
    47.     [Tooltip("Make sure you assign them in leaf to root order only...")]
    48.     [SerializeField]
    49.     public Bone[] bones;
    50.     [Tooltip("The end point of the leaf bone positioned at tip of the chain to get its orientation...")]
    51.     public Transform endPointOfLastBone;
    52.  
    53.     [Header("Settings")]
    54.     [Tooltip("Bend chain towards this target when target is closer than total chain length... Make sure you place it far than total chain length for better accuracy...")]
    55.     public Transform poleTarget;
    56.     [Tooltip("More precision...")]
    57.     public int iterations;
    58.  
    59.     [Header("EditMode")]
    60.     public bool enable;
    61.  
    62.     [HideInInspector]
    63.     public bool needResetOption = false;
    64.  
    65.     private Vector3 lastTargetPosition;
    66.     private bool editorInitialized = false;
    67.  
    68.     void Start()
    69.     {
    70.         lastTargetPosition = transform.position;
    71.         if (Application.isPlaying && !editorInitialized)
    72.         {
    73.             Initialize();
    74.         }
    75.     }
    76.  
     
  2. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    The editor class belongs into its own file into an editor folder (a folder called Editor)
     
    renman3000 likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    It's an editor script. Those are NEVER attached to GameObjects. Go read about them. As Malz points out they must be in an Editor/ folder or else they will prevent you from building.
     
    renman3000 likes this.
  4. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Thanks!