private void Start() { records = JsonConvert.DeserializeObject <Records>( File.ReadAllText(Path.Combine(ImageDirectory, "records.json"))); foreach (Record record in records.records) { string filepath = Path.Combine(ImageDirectory, record.name + ".png"); if (!File.Exists(filepath)) { continue; } // Instantiate the image. ModelImage img = Instantiate(imagePrefab.gameObject).GetComponent <ModelImage>(); // Parent this image to the scroll view. img.GetComponent <RectTransform>().SetParent(content); // Set the image. Texture2D tex = GetMaterialImage(filepath); img.image.sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f)); img.text.text = record.name; // Store the image and model name. images.Add(img, record); } loading.gameObject.SetActive(false); buttonQuit.onClick.AddListener(() => Application.Quit()); PopulateDropdownSearch(); dropdownSearch.onValueChanged.AddListener(SelectSearchType); inputSearch.onValueChanged.AddListener(FilterByName); // Set a defaul value. dropdownSearch.value = 0; // Listen to events. ModelImage.OnSelect += Select; ModelImage.OnDeselect += Deselect; }
/// <summary> /// Get all model images. Only do this at start! /// </summary> private void PopulateModelImages() { DirectoryInfo d = new DirectoryInfo(ImageDirectory); foreach (FileInfo f in d.GetFiles()) { string modelName = f.Name.Replace("\r", "").Replace(".jpg", "").Replace(".png", ""); // Ignore blanks. if (modelName == "") { continue; } // Set the image. Texture2D tex = GetModelImage(f.FullName); if (tex == null) { continue; } // Instantiate the image. ModelImage img = Instantiate(modelImagePrefab.gameObject).GetComponent <ModelImage>(); // Parent this image to the scroll view. img.GetComponent <RectTransform>().SetParent(imageScrollView); img.image.sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f)); img.text.text = modelName; // Store the image and model name. models.Add(img, modelName); } // Apply filter. dropdownWnids.value = DEFAULT_WNID_FILTER; // Listen to events. ModelImage.OnSelect += Select; ModelImage.OnDeselect += Deselect; searchBar.onValueChanged.AddListener(Search); }