Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Help Edit Script

Discussion in 'General Discussion' started by lxtopxm, Jan 31, 2021.

  1. lxtopxm

    lxtopxm

    Joined:
    Jan 3, 2014
    Posts:
    2
    Help rewrite the script

    Multi-object editing not supported


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEditor;
    6. namespace Atavism
    7. {
    8.     public enum HarvestType
    9.     {
    10.         Axe,
    11.         Pickaxe,
    12.         None
    13.     }
    14.    
    15.     public class ResourceNode : MonoBehaviour
    16.     {
    17.  
    18.         public int id = -1;
    19.         public List<ResourceDrop> resources;
    20.         public int resourceCount = 1;
    21.         int currentResourceCount;
    22.         public string harvestTool;
    23.         bool toolMustBeEquipped = true;
    24.         public float timeToHarvest = 0;
    25.         public int skillType = -1;
    26.         public int reqSkillLevel = 0;
    27.         public int skillLevelMax = 10;
    28.         public int skillExp = 0;
    29.         public float cooldown = 2;
    30.         float cooldownEnds;
    31.         public float refreshDuration = 60;
    32.         public Texture2D cursorIcon;
    33.         public bool highlight = true;
    34.         public Color highlightColour = Color.cyan;
    35.         public Sprite selectedIcon;
    36.  
    37.         public GameObject harvestCoordEffect;
    38.         public GameObject activateCoordEffect;
    39.         public GameObject deactivateCoordEffect;
    40.  
    41.         public bool isLODChild = false;
    42.  
    43.         Color initialColor;
    44.         bool active = true;
    45.         bool selected = false;
    46.         Renderer[] renderers;
    47.         Color[] initialColors;
    48.         bool mouseOver = false;
    49.  
    50.         // Use this for initialization
    51.         void Start()
    52.         {
    53.             cooldownEnds = Time.time;
    54.             currentResourceCount = resourceCount;
    55.             gameObject.AddComponent<AtavismNode>();
    56.             GetComponent<AtavismNode>().AddLocalProperty("harvestType", harvestTool);
    57.             GetComponent<AtavismNode>().AddLocalProperty("targetable", false);
    58.             GetComponent<AtavismNode>().AddLocalProperty("active", active);
    59.             if (highlight)
    60.                 if (GetComponent<Renderer>() != null)
    61.                 {
    62.                     if (GetComponent<Renderer>().material.HasProperty("_Color"))
    63.                         initialColor = GetComponent<Renderer>().material.color;
    64.                 }
    65.                 else
    66.                 {
    67.                     renderers = GetComponentsInChildren<Renderer>();
    68.                     initialColors = new Color[renderers.Length];
    69.                     for (int i = 0; i < renderers.Length; i++)
    70.                     {
    71.                         initialColors[i] = renderers[i].material.color;
    72.                     }
    73.                 }
    74.  
    75.             // Add child component to all children with colliders
    76.             foreach (Collider child in GetComponentsInChildren<Collider>())
    77.             {
    78.                 if (child.gameObject != gameObject)
    79.                     child.gameObject.AddComponent<ObjectChildMouseDetector>();
    80.             }
    81.  
    82.             Crafting.Instance.RegisterResourceNode(this);
    83.         }
    84.  
    85.         void OnDestroy()
    86.         {
    87.             if (ClientAPI.ScriptObject != null)
    88.                 Crafting.Instance.RemoveResourceNode(id);
    89.             AtavismCursor.Instance.ClearMouseOverObject(GetComponent<AtavismNode>());
    90.             mouseOver = false;
    91.         }
    92.  
    93.         void OnDisable()
    94.         {
    95.             AtavismCursor.Instance.ClearMouseOverObject(GetComponent<AtavismNode>());
    96.             mouseOver = false;
    97.         }
    98.  
    99.         // Update is called once per frame
    100.         void Update()
    101.         {
    102.             if (mouseOver)
    103.             {
    104.                 if (AtavismCursor.Instance.IsMouseOverUI())
    105.                 {
    106.                     ResetHighlight();
    107.                     AtavismCursor.Instance.ClearMouseOverObject(GetComponent<AtavismNode>());
    108.                 }
    109.                 else
    110.                 {
    111.                     Highlight();
    112.                     AtavismCursor.Instance.SetMouseOverObject(GetComponent<AtavismNode>(), cursorIcon, 4);
    113.                 }
    114.                 if (Input.GetMouseButtonDown(1) && !AtavismCursor.Instance.IsMouseOverUI())
    115.                 {
    116.                     HarvestResource();
    117.                 }
    118.             }
    119.         }
    120.  
    121.         void OnMouseDown()
    122.         {
    123.             if (!AtavismCursor.Instance.IsMouseOverUI() && !(ClientAPI.GetInputController() is ClickToMoveInputController))
    124.                 HarvestResource();
    125.         }
    126.  
    127.         void OnMouseOver()
    128.         {
    129.             if (active)
    130.             {
    131.                 AtavismCursor.Instance.SetMouseOverObject(GetComponent<AtavismNode>(), cursorIcon, 4);
    132.                 Highlight();
    133.             }
    134.             mouseOver = true;
    135.         }
    136.  
    137.         void OnMouseExit()
    138.         {
    139.             AtavismCursor.Instance.ClearMouseOverObject(GetComponent<AtavismNode>());
    140.             if (!selected)
    141.                 ResetHighlight();
    142.             mouseOver = false;
    143.         }
    144.  
    145.         public void HarvestResource()
    146.         {
    147.             if (Time.time < cooldownEnds)
    148.             {
    149.                 // Send error message
    150.                 string[] args = new string[1];
    151. #if AT_I2LOC_PRESET
    152.             args[0] = I2.Loc.LocalizationManager.GetTranslation("You cannot perform that action yet");
    153. #else
    154.                 args[0] = "You cannot perform that action yet.";
    155. #endif
    156.                 AtavismEventSystem.DispatchEvent("ERROR_MESSAGE", args);
    157.             }
    158.             else
    159.             {
    160.                 Crafting.Instance.HarvestResource(id);
    161.                 cooldownEnds = Time.time + cooldown;
    162.                 AtavismLogger.LogInfoMessage("Sending harvest resource");
    163.             }
    164.         }
    165.  
    166.         public void Highlight()
    167.         {
    168.             if (!highlight)
    169.                 return;
    170.             if (GetComponent<Renderer>() != null)
    171.             {
    172.                 if (GetComponent<Renderer>().material.HasProperty("color"))
    173.                     GetComponent<Renderer>().material.color = highlightColour;
    174.             }
    175.             else
    176.             {
    177.                 for (int i = 0; i < renderers.Length; i++)
    178.                 {
    179.                     if (renderers[i].material.HasProperty("color"))
    180.                         renderers[i].material.color = highlightColour;
    181.                 }
    182.             }
    183.         }
    184.  
    185.         public void ResetHighlight()
    186.         {
    187.             if (!highlight)
    188.                 return;
    189.             if (GetComponent<Renderer>() != null)
    190.             {
    191.                 if (GetComponent<Renderer>().material.HasProperty("color"))
    192.                     GetComponent<Renderer>().material.color = initialColor;
    193.             }
    194.             else
    195.             {
    196.                 for (int i = 0; i < renderers.Length; i++)
    197.                 {
    198.                     if (renderers[i].material.HasProperty("color"))
    199.                         renderers[i].material.color = initialColors[i];
    200.                 }
    201.             }
    202.         }
    203.  
    204.         public int ID
    205.         {
    206.             set
    207.             {
    208.                 id = value;
    209.             }
    210.         }
    211.  
    212.         public bool ToolMustBeEquipped
    213.         {
    214.             get
    215.             {
    216.                 return toolMustBeEquipped;
    217.             }
    218.             set
    219.             {
    220.                 toolMustBeEquipped = value;
    221.             }
    222.         }
    223.  
    224.         /// <summary>
    225.         /// Gets or sets a value indicating whether this <see cref="ResourceNode"/> is active.
    226.         /// </summary>
    227.         /// <value><c>true</c> if active; otherwise, <c>false</c>.</value>
    228.         public bool Active
    229.         {
    230.             get
    231.             {
    232.                 return active;
    233.             }
    234.             set
    235.             {
    236.                 if (value != active)
    237.                 {
    238.                     //Debug.LogError("ResourceNode: harvestCoordEffect="+ harvestCoordEffect+" activateCoordEffect = " + activateCoordEffect + " deactivateCoordEffect=" + deactivateCoordEffect);
    239.                     if (value && activateCoordEffect != null)
    240.                     {
    241.                         Dictionary<string, object> props = new Dictionary<string, object>();
    242.                         props.Add("gameObject", gameObject);
    243.                         CoordinatedEffectSystem.ExecuteCoordinatedEffect(activateCoordEffect.name, props);
    244.                     }
    245.                     else if (!value && deactivateCoordEffect != null)
    246.                     {
    247.                         Dictionary<string, object> props = new Dictionary<string, object>();
    248.                         props.Add("gameObject", gameObject);
    249.                         CoordinatedEffectSystem.ExecuteCoordinatedEffect(deactivateCoordEffect.name, props);
    250.                     }
    251.                     if (GetComponent<bl_MiniMapItem>() != null)
    252.                         if (active == false)
    253.                         {
    254.                             GetComponent<bl_MiniMapItem>().HideItem();
    255.                         }
    256.                         else
    257.                         {
    258.                             GetComponent<bl_MiniMapItem>().ShowItem();
    259.                         }
    260.  
    261.                 }
    262.                 active = value;
    263.                 GetComponent<AtavismNode>().AddLocalProperty("active", active);
    264.                 if (GetComponent<MeshRenderer>() != null)
    265.                 {
    266.                     GetComponent<MeshRenderer>().enabled = active;
    267.                     GetComponent<Collider>().enabled = active;
    268.                 }
    269.                 foreach (Transform child in GetComponent<Transform>())
    270.                 {
    271.                     if (child.GetComponent<MeshRenderer>() != null)
    272.                     {
    273.                         child.GetComponent<MeshRenderer>().enabled = active;
    274.                     }
    275.                     if (child.GetComponent<Collider>() != null)
    276.                     {
    277.                         child.GetComponent<Collider>().enabled = active;
    278.                     }
    279.                     child.gameObject.SetActive(active);
    280.                 }
    281.                 /*if (active == false) {
    282.                     mouseOver = false;
    283.                 }*/
    284.             }
    285.         }
    286.  
    287.     }
    288. }
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    This indicates an *issue with a custom editor script, which this MonoBehavior script is not.
    Go into your custom editor script and just add the [CanEditMultipleObjects] attribute.

    *Not really an issue, just an option that wasn't enabled.
     
    MadeFromPolygons likes this.