My game consists of several “features” the player can select using a button prefab and swap out sprites (a character creator). I can click and drag any of these parts around, but I want to be able to ONLY move the feature that is currently selected. My feature manager script is:
- using UnityEngine;
- using System.Collections.Generic;
-
- [ExecuteInEditMode]
- public class FeatureManager : MonoBehaviour
- {
- public List features;
- public int currFeature;
-
-
- void OnEnable()
-
- {
- LoadFeatures();
- }
- void OnDisable()
- {
- SaveFeatures();
- }
-
- void Update()
- {
- if (Input.GetKey(“escape”))
- Application.Quit();
- }
-
-
- void LoadFeatures()
-
- {
- features = new List();
-
- features.Add(new Feature(“Face”, transform.Find(“Face”).GetComponent()));
- features.Add(new Feature(“Eyes”, transform.Find(“Eyes”).GetComponent()));
- features.Add(new Feature(“Arms”, transform.Find(“Arms”).GetComponent()));
- features.Add(new Feature(“Horns”, transform.Find(“Horns”).GetComponent()));
- features.Add(new Feature(“Mouths”, transform.Find(“Mouths”).GetComponent()));
- features.Add(new Feature(“Noses”, transform.Find(“Noses”).GetComponent()));
- features.Add(new Feature(“Tails”, transform.Find(“Tails”).GetComponent()));
- features.Add(new Feature(“Body”, transform.Find(“Body”).GetComponent()));
- features.Add(new Feature(“Legs”, transform.Find(“Legs”).GetComponent()));
-
-
-
- for (int i = 0; i < features.Count; i++)
-
-
- {
- string key = “FEATURE_” + i;
- if (!PlayerPrefs.HasKey(key))
- PlayerPrefs.SetInt(key, features*.currIndex);*
- features*.currIndex = PlayerPrefs.GetInt(key);*
- features*.UpdateFeature ();*
- }
- }
- void SaveFeatures()
- {
- for (int i = 0; i < features.Count; i++)
- {
- string key = “FEATURE_” + i;
- PlayerPrefs.SetInt (key, features .currIndex);
- }
- PlayerPrefs.Save ();
- }
-
- public void SetCurrent(int index)
- {
- if (features == null)
- return;
-
- currFeature = index;
- }
- public void NextChoice()
- {
- if (features == null)
- return;
- features[currFeature].currIndex++;
- features [currFeature].UpdateFeature();
- }
- public void PreviousChoice()
- {
- if (features == null)
- return;
- features[currFeature].currIndex–;
- features [currFeature].UpdateFeature();
- }
- }
-
- [System.Serializable]
- public class Feature
- {
- public string ID;
- public int currIndex;
- public Sprite[ ] choices;
- public SpriteRenderer renderer;
-
- public Feature(string id, SpriteRenderer rend)
- {
- ID = id;
- renderer = rend;
- UpdateFeature();
- }
-
- public void UpdateFeature()
- {
- choices = Resources.LoadAll(“Textures/” + ID);
-
- if (choices == null || renderer == null)
- return;
-
- if (currIndex < 0)
- currIndex = choices.Length - 1;
- if (currIndex >= choices.Length)
- currIndex = 0;
-
- renderer.sprite = choices[currIndex];
- }
- }
And my MouseManager script is:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class MouseManager : MonoBehaviour {
-
- private Vector3 offset;
-
- void OnMouseDown()
- {
-
- offset = gameObject.transform.position -
- Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10.0f));
- }
-
- void OnMouseDrag()
- {
- Vector3 newPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10.0f);
- transform.position = Camera.main.ScreenToWorldPoint(newPosition) + offset;
- }
- }
Everything is working great, I just want it so that the current selected feature (i.e. “arms”) is the only thing that can be dragged. I’m not sure where to plug in the mouse manager coding to work with the selected feature coding. Thanks in advance for the help!