Search Unity

Question I am beginner, And I am stuck In this error, Can I get help? SocketEditor.cs (140,13): error CS0117:

Discussion in 'Scripting' started by amitjay230, Nov 27, 2022.

  1. amitjay230

    amitjay230

    Joined:
    Nov 8, 2022
    Posts:
    1
    SocketEditor.cs (140,13): error CS0117: handle does not contain a definition of sphereCap




    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEditorInternal;
    3. using UltimateSurvival.Building;
    4.  
    5. namespace UltimateSurvival.Editor
    6. {
    7.     using UnityEditor;
    8.     using UnityEngine;
    9.  
    10.     [CustomEditor(typeof(Socket))]
    11.     [CanEditMultipleObjects]
    12.     public class SocketEditor : Editor
    13.     {
    14.         private static bool m_InitializedSelector;
    15.         private static Socket m_SelectedSocket;
    16.         private static BuildingPiece m_SelectedPiece;
    17.  
    18.         private static Material m_PreviewMat;
    19.  
    20.         private Socket m_Socket;
    21.  
    22.         private bool m_EditOffset;
    23.         private SerializedProperty m_Radius;
    24.         private int m_SelectedPieceIdx;
    25.  
    26.         private ReorderableList m_PieceOffsets;
    27.  
    28.         private Socket.PieceOffset m_SelectedPieceOffset;
    29.    
    30.  
    31.         public override void OnInspectorGUI ()
    32.         {
    33.             //base.OnInspectorGUI();
    34.  
    35.             serializedObject.Update();
    36.  
    37.             if(Application.isPlaying)
    38.             {
    39.                 GUILayout.Label("Occupied Spaces: ", EditorStyles.boldLabel);
    40.                 EditorGUILayout.Space();
    41.  
    42.                 for(int i = 0;i < m_Socket.OccupiedSpaces.Count;i ++)
    43.                     GUILayout.Label(m_Socket.OccupiedSpaces[i].ToString());
    44.             }
    45.  
    46.             EditorGUILayout.Space();
    47.             EditorGUILayout.PropertyField(m_Radius);
    48.             CustomGUI.DoHorizontalLine();
    49.             EditorGUILayout.Space();
    50.  
    51.             GUI.color = m_EditOffset ? Color.grey : Color.white;
    52.  
    53.             if(GUILayout.Button("Edit Piece Offset"))
    54.             {
    55.                 m_EditOffset = !m_EditOffset;
    56.  
    57.                 if (!m_EditOffset)
    58.                     Tools.current = Tool.Move;
    59.             }
    60.  
    61.             GUI.color = Color.white;
    62.  
    63.             if(!serializedObject.isEditingMultipleObjects)
    64.                 m_PieceOffsets.DoLayoutList();
    65.            
    66.             EditorGUILayout.Space();
    67.  
    68.             serializedObject.ApplyModifiedProperties();
    69.         }
    70.  
    71.         private void OnEnable()
    72.         {
    73.             // Create the material for the preview if it's null.
    74.             if(m_PreviewMat == null)
    75.             {
    76.                 m_PreviewMat = new Material(Shader.Find("Transparent/Diffuse"));
    77.                 m_PreviewMat.color = new Color(0.5f, 0.6f, 0.5f, 1f);
    78.             }
    79.  
    80.             m_Socket = target as Socket;
    81.  
    82.             m_Radius = serializedObject.FindProperty("m_Radius");
    83.  
    84.             // Initialize the piece list.
    85.             m_PieceOffsets = new ReorderableList(serializedObject, serializedObject.FindProperty("m_PieceOffsets"));
    86.  
    87.             m_PieceOffsets.drawHeaderCallback = (Rect rect)=> GUI.Label(rect, "Supported Pieces");
    88.             m_PieceOffsets.drawElementCallback = DrawPieceElement;
    89.             m_PieceOffsets.onSelectCallback += OnPieceSelect;
    90.         }
    91.  
    92.         private void OnDestroy() { Tools.hidden = false; }
    93.  
    94.         private void DrawPieceElement(Rect rect, int index, bool isActive, bool isFocused)
    95.         {
    96.             EditorGUI.BeginChangeCheck();
    97.  
    98.             EditorGUI.PropertyField(new Rect(rect.x, rect.y, rect.width, 16f), m_PieceOffsets.serializedProperty.GetArrayElementAtIndex(index).FindPropertyRelative("m_Piece"));
    99.  
    100.             if(EditorGUI.EndChangeCheck())
    101.                 TrySelectPiece(index);
    102.         }
    103.  
    104.         private void OnPieceSelect(ReorderableList list) { TrySelectPiece(list.index); }
    105.  
    106.         private void TrySelectPiece(int index)
    107.         {
    108.             if(m_PieceOffsets.count > 0)
    109.             {
    110.                 index = Mathf.Clamp(index, 0, m_PieceOffsets.count - 1);
    111.  
    112.                 m_SelectedPieceOffset = (target as Socket).PieceOffsets[index];
    113.  
    114.                 m_PieceOffsets.index = index;
    115.                 m_SelectedPieceIdx = index;
    116.             }
    117.         }
    118.  
    119.         private void OnSceneGUI()
    120.         {
    121.             Tools.hidden = m_EditOffset;
    122.  
    123.             if (CannotDisplayMesh())
    124.             {
    125.                 SceneView.RepaintAll();
    126.  
    127.                 return;
    128.             }
    129.  
    130.             // Get the scene camera and it's pixel rect.
    131.             var sceneCamera = SceneView.GetAllSceneCameras()[0];
    132.             Rect pixelRect = sceneCamera.pixelRect;
    133.  
    134.             if(HasValidPiece())
    135.             {
    136.                 // Draw the piece handle.
    137.                 Vector3 pieceWorldPos = m_Socket.transform.position + m_Socket.transform.TransformVector(m_SelectedPieceOffset.PositionOffset);
    138.    
    139.                 Handles.color = Color.grey;
    140.                 Handles.SphereCap(GUIUtility.GetControlID(FocusType.Passive), pieceWorldPos, m_Socket.transform.rotation, 0.15f);
    141.  
    142.                 if(m_SelectedPieceOffset.Piece.MainMesh == null)
    143.                 {
    144.                     Debug.LogError("You have to assign the Main Mesh for this BuildingPiece: '" + m_SelectedPieceOffset.Piece.name + "'", this);
    145.                     return;
    146.                 }
    147.  
    148.                 var mesh = m_SelectedPieceOffset.Piece.MainMesh.sharedMesh;
    149.  
    150.                 // HACK
    151.                 try
    152.                 {
    153.                     Vector3 position = m_Socket.transform.position + m_Socket.transform.TransformVector(m_Socket.PieceOffsets[m_SelectedPieceIdx].PositionOffset);
    154.                     Quaternion rotation = m_Socket.transform.rotation * m_Socket.PieceOffsets[m_SelectedPieceIdx].RotationOffset;
    155.                     Vector3 scale = m_SelectedPieceOffset.Piece.transform.lossyScale;
    156.  
    157.                     for (int m = 0; m < mesh.subMeshCount; m++)
    158.                         Graphics.DrawMesh(mesh, Matrix4x4.TRS(position, rotation, scale), m_PreviewMat, 0, sceneCamera, m);
    159.                 } catch {}
    160.  
    161.                 SceneView.RepaintAll();
    162.  
    163.                 // Draw the piece tools (move & rotate).
    164.                 DoPieceOffsetTools();
    165.             }
    166.  
    167.             // Draw the inspector for the piece offset for the selected socket, so you can modify the position and rotation precisely.
    168.             DoPieceOffsetInspectorWindow(pixelRect);
    169.  
    170.             //Debug.Log(m_SelectedPieceOffset);
    171.         }
    172.  
    173.         private void DoPieceOffsetTools()
    174.         {
    175.             Vector3 pieceWorldPos = m_Socket.transform.position + m_Socket.transform.TransformVector(m_SelectedPieceOffset.PositionOffset);
    176.  
    177.             EditorGUI.BeginChangeCheck();
    178.             var handlePos = Handles.PositionHandle(pieceWorldPos, m_Socket.transform.rotation * m_SelectedPieceOffset.RotationOffset);
    179.  
    180.             if(EditorGUI.EndChangeCheck())
    181.             {
    182.                 Undo.RecordObject(target, "Socket");
    183.  
    184.                 handlePos = RoundVector3(m_Socket.transform.InverseTransformPoint(handlePos));
    185.                 m_SelectedPieceOffset.PositionOffset = handlePos;
    186.             }
    187.         }
    188.  
    189.         private void DoPieceOffsetInspectorWindow(Rect pixelRect)
    190.         {
    191.             Color color = Color.white;
    192.             GUI.backgroundColor = color;
    193.  
    194.             var windowRect = new Rect(16f, 32f, 256f, 112f);
    195.             Rect totalRect = new Rect(windowRect.x, windowRect.y - 16f, windowRect.width, windowRect.height);
    196.  
    197.             GUI.backgroundColor = Color.white;
    198.             GUI.Window(1, windowRect, DrawPieceOffsetInspector, "Piece Offset");
    199.  
    200.             Event e = Event.current;
    201.  
    202.             if(totalRect.Contains(e.mousePosition))
    203.             {
    204.                 HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive));
    205.  
    206.                 if(e.type != EventType.Layout && e.type != EventType.Repaint)
    207.                     e.Use();
    208.             }
    209.         }
    210.  
    211.         private void DrawPieceOffsetInspector(int windowID)
    212.         {
    213.             if(!HasValidPiece())
    214.             {
    215.                 EditorGUI.HelpBox(new Rect(0f, 32f, 512f, 32f), "No valid piece selected!", MessageType.Warning);
    216.                 return;
    217.             }
    218.                
    219.             var pieceOffset = m_SelectedPieceOffset;
    220.  
    221.             EditorGUI.BeginChangeCheck();
    222.  
    223.             // Position field.
    224.             var positionOffset = EditorGUI.Vector3Field(new Rect(6f, 32f, 240f, 16f), "Position", pieceOffset.PositionOffset);
    225.  
    226.             // Rotation field.
    227.             var rotationOffset = EditorGUI.Vector3Field(new Rect(6f, 64f, 240f, 16f), "Rotation", pieceOffset.RotationOffsetEuler);
    228.  
    229.             if(EditorGUI.EndChangeCheck())
    230.             {
    231.                 Undo.RecordObject(target, "Socket");
    232.  
    233.                 positionOffset = RoundVector3(positionOffset);
    234.                 rotationOffset = RoundVector3(rotationOffset);
    235.  
    236.                 pieceOffset.PositionOffset = positionOffset;
    237.                 pieceOffset.RotationOffsetEuler = rotationOffset;
    238.             }
    239.         }
    240.  
    241.         private bool HasValidPiece() { return m_PieceOffsets.count != 0 && m_SelectedPieceIdx >= 0 && m_SelectedPieceOffset != null && m_SelectedPieceOffset.Piece != null; }
    242.  
    243.         private bool CannotDisplayMesh() { return (!m_EditOffset || Selection.activeGameObject == null || Selection.activeGameObject != m_Socket.gameObject); }
    244.  
    245.         private Vector3 RoundVector3(Vector3 source, int digits = 3)
    246.         {
    247.             source.x = (float)System.Math.Round(source.x, digits);
    248.             if(Mathf.Approximately(source.x, 0f))
    249.                 source.x = 0f;
    250.  
    251.             source.y = (float)System.Math.Round(source.y, digits);
    252.             if(Mathf.Approximately(source.y, 0f))
    253.                 source.y = 0f;
    254.  
    255.             source.z = (float)System.Math.Round(source.z, digits);
    256.             if(Mathf.Approximately(source.y, 0f))
    257.                 source.y = 0f;
    258.  
    259.             return source;
    260.         }
    261.     }
    262. }
    263.  
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    I don't know what Handles is, but the error tells you that it doesn't contain the method sphereCap. So, you need to double check that.

    Remember that caps matter, as that might be your error.
     
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,861
    Did you write this? Or is this code from something off the asset store?

    Feels like you may be using something in a too new or too old version of Unity. It should probably be
    SphereHandleCap
    .
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Is this namespace yours? Or is this part of another package? If it's another package, sounds like either you didn't get all the pieces of it, or as others point out above, it is out of date.

    Another problem is this:

    Using internal stuff in the editor sets you up for those internals to change, and when they change your code will break. This might be fixed by what @spiney199 points out above, but I am not familiar with any editor internals, as I stay away from anything except the public API.