示例#1
0
        static void LoadSkins()
        {
            foreach (var prefab in prefabMap)
            {
                Skin defSkin = CreateDefaultSkin(prefab.Key, prefab.Value);
                defaultSkins.Add(prefab.Key, defSkin);

                var dir = modPath + "Skins\\" + prefab.Value;

                if (Directory.Exists(dir))
                {
                    var            subDirectories = Directory.GetDirectories(dir);
                    var            skinGroup      = new SkinGroup(prefab.Key);
                    var            carPrefab      = CarTypes.GetCarPrefab(prefab.Key);
                    var            cmps           = carPrefab.gameObject.GetComponentsInChildren <MeshRenderer>();
                    MeshRenderer[] interiorCmps   = null;

                    var trainCar = carPrefab.GetComponent <TrainCar>();

                    if (trainCar.interiorPrefab != null)
                    {
                        interiorCmps = trainCar.interiorPrefab.GetComponentsInChildren <MeshRenderer>();
                    }

                    foreach (var subDir in subDirectories)
                    {
                        var dirInfo = new DirectoryInfo(subDir);
                        var files   = Directory.GetFiles(subDir);
                        var skin    = new Skin(dirInfo.Name);

                        foreach (var file in files)
                        {
                            FileInfo fileInfo = new FileInfo(file);
                            string   fileName = Path.GetFileNameWithoutExtension(fileInfo.Name);
                            byte[]   fileData = File.ReadAllBytes(file);

                            foreach (var cmp in cmps)
                            {
                                if (!cmp.material)
                                {
                                    continue;
                                }

                                var diffuse  = GetMaterialTexture(cmp, "_MainTex");
                                var normal   = GetMaterialTexture(cmp, "_BumpMap");
                                var specular = GetMaterialTexture(cmp, "_MetallicGlossMap");
                                var emission = GetMaterialTexture(cmp, "_EmissionMap");

                                if (diffuse != null)
                                {
                                    if ((diffuse.name == fileName || aliasNames.ContainsKey(diffuse.name) && aliasNames[diffuse.name] == fileName) && !skin.ContainsTexture(diffuse.name))
                                    {
                                        var texture = new Texture2D(diffuse.width, diffuse.height);
                                        texture.name = fileName;

                                        texture.LoadImage(fileData);
                                        texture.Apply(true, true);

                                        SetTextureOptions(texture);

                                        skin.skinTextures.Add(new SkinTexture(diffuse.name, texture));
                                    }
                                }

                                if (normal != null)
                                {
                                    if ((normal.name == fileName || aliasNames.ContainsKey(normal.name) && aliasNames[normal.name] == fileName) && !skin.ContainsTexture(normal.name))
                                    {
                                        var texture = new Texture2D(normal.width, normal.height, TextureFormat.ARGB32, true, true);
                                        texture.name = fileName;

                                        texture.LoadImage(fileData);
                                        texture.Apply(true, true);

                                        SetTextureOptions(texture);

                                        skin.skinTextures.Add(new SkinTexture(normal.name, texture));
                                    }
                                }


                                if (specular != null)
                                {
                                    if ((specular.name == fileName || aliasNames.ContainsKey(specular.name) && aliasNames[specular.name] == fileName) && !skin.ContainsTexture(specular.name))
                                    {
                                        var texture = new Texture2D(specular.width, specular.height);
                                        texture.name = fileName;

                                        texture.LoadImage(fileData);
                                        texture.Apply(true, true);

                                        SetTextureOptions(texture);

                                        skin.skinTextures.Add(new SkinTexture(specular.name, texture));
                                    }
                                }

                                if (emission != null)
                                {
                                    if ((emission.name == fileName || aliasNames.ContainsKey(emission.name) && aliasNames[emission.name] == fileName) && !skin.ContainsTexture(emission.name))
                                    {
                                        var texture = new Texture2D(emission.width, emission.height);
                                        texture.name = fileName;

                                        texture.LoadImage(fileData);
                                        texture.Apply(true, true);

                                        SetTextureOptions(texture);

                                        skin.skinTextures.Add(new SkinTexture(emission.name, texture));
                                    }
                                }
                            }

                            if (interiorCmps != null)
                            {
                                foreach (var cmp in interiorCmps)
                                {
                                    if (!cmp.material)
                                    {
                                        continue;
                                    }

                                    var diffuse  = GetMaterialTexture(cmp, "_MainTex");
                                    var normal   = GetMaterialTexture(cmp, "_BumpMap");
                                    var specular = GetMaterialTexture(cmp, "_MetallicGlossMap");
                                    var emission = GetMaterialTexture(cmp, "_EmissionMap");

                                    if (diffuse != null)
                                    {
                                        if ((diffuse.name == fileName || aliasNames.ContainsKey(diffuse.name) && aliasNames[diffuse.name] == fileName) && !skin.ContainsTexture(diffuse.name))
                                        {
                                            var texture = new Texture2D(diffuse.width, diffuse.height);
                                            texture.name = fileName;

                                            texture.LoadImage(fileData);
                                            texture.Apply(true, true);

                                            SetTextureOptions(texture);

                                            skin.skinTextures.Add(new SkinTexture(diffuse.name, texture));
                                        }
                                    }

                                    if (normal != null)
                                    {
                                        if ((normal.name == fileName || aliasNames.ContainsKey(normal.name) && aliasNames[normal.name] == fileName) && !skin.ContainsTexture(normal.name))
                                        {
                                            var texture = new Texture2D(normal.width, normal.height, TextureFormat.ARGB32, true, true);
                                            texture.name = fileName;

                                            texture.LoadImage(fileData);
                                            texture.Apply(true, true);

                                            SetTextureOptions(texture);

                                            skin.skinTextures.Add(new SkinTexture(normal.name, texture));
                                        }
                                    }


                                    if (specular != null)
                                    {
                                        if ((specular.name == fileName || aliasNames.ContainsKey(specular.name) && aliasNames[specular.name] == fileName) && !skin.ContainsTexture(specular.name))
                                        {
                                            var texture = new Texture2D(specular.width, specular.height);
                                            texture.name = fileName;

                                            texture.LoadImage(fileData);
                                            texture.Apply(true, true);

                                            SetTextureOptions(texture);

                                            skin.skinTextures.Add(new SkinTexture(specular.name, texture));
                                        }
                                    }

                                    if (emission != null)
                                    {
                                        if ((emission.name == fileName || aliasNames.ContainsKey(emission.name) && aliasNames[emission.name] == fileName) && !skin.ContainsTexture(emission.name))
                                        {
                                            var texture = new Texture2D(emission.width, emission.height);
                                            texture.name = fileName;

                                            texture.LoadImage(fileData);
                                            texture.Apply(true, true);

                                            SetTextureOptions(texture);

                                            skin.skinTextures.Add(new SkinTexture(emission.name, texture));
                                        }
                                    }
                                }
                            }
                        }

                        skinGroup.skins.Add(skin);
                    }

                    skinGroups.Add(prefab.Key, skinGroup);
                }
            }
        }
示例#2
0
        public static void DrawModUI()
        {
            if (!isEnabled)
            {
                showDropdown = false;
                return;
            }

            Cursor.lockState = CursorLockMode.None;
            Cursor.visible   = true;

            GameObject   carToSpawn = SingletonBehaviour <CarSpawner> .Instance.carToSpawn;
            TrainCar     trainCar   = carToSpawn.GetComponent <TrainCar>();
            TrainCarType carType    = trainCar.carType;

            SkinGroup skinGroup  = Main.skinGroups[carType];
            string    selectSkin = Main.selectedSkin[carType];

            float menuHeight = 60f;

            float menuWidth   = 270f;
            float buttonWidth = 240f;
            bool  isMaxHeight = false;
            float maxHeight   = Screen.height - 200f;

            if (showDropdown)
            {
                float totalItemHeight = skinGroup.skins.Count * 23f;

                if (totalItemHeight > maxHeight)
                {
                    totalItemHeight = maxHeight;
                    isMaxHeight     = true;
                }

                menuHeight += totalItemHeight + 46f;
            }

            if (isMaxHeight)
            {
                buttonWidth -= 20f;
            }

            GUI.skin = DVGUI.skin;
            GUI.skin.label.fontSize  = 11;
            GUI.skin.button.fontSize = 10;
            GUI.color = new Color32(0, 0, 0, 200);
            GUI.Box(new Rect(20f, 20f, menuWidth, menuHeight), "");
            GUILayout.BeginArea(new Rect(30f, 20f, menuWidth, menuHeight));
            GUI.color = Color.yellow;
            GUILayout.Label("Skin Manager Menu :: " + carToSpawn.name);
            GUI.color = Color.white;
            GUILayout.Space(5f);

            if (showDropdown)
            {
                if (GUILayout.Button("=== " + selectSkin + " ===", GUILayout.Width(240f)))
                {
                    showDropdown = false;
                }

                if (isMaxHeight)
                {
                    scrollViewVector = GUILayout.BeginScrollView(scrollViewVector, GUILayout.Width(245f), GUILayout.Height(menuHeight - 55f));
                }

                if (GUILayout.Button("Random", GUILayout.Width(buttonWidth)))
                {
                    showDropdown = false;
                    Main.selectedSkin[carType] = "Random";
                }

                if (GUILayout.Button("Default", GUILayout.Width(buttonWidth)))
                {
                    showDropdown = false;
                    Main.selectedSkin[carType] = "Default";
                }

                foreach (Skin skin in skinGroup.skins)
                {
                    if (GUILayout.Button(skin.name, GUILayout.Width(buttonWidth)))
                    {
                        showDropdown = false;
                        Main.selectedSkin[carType] = skin.name;
                    }
                }

                if (isMaxHeight)
                {
                    GUILayout.EndScrollView();
                }
            }
            else
            {
                if (GUILayout.Button("=== " + selectSkin + " ===", GUILayout.Width(240f)))
                {
                    showDropdown = true;
                }
            }

            GUILayout.EndArea();
        }