Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by nickkunes · Sep 05, 2012 at 11:26 PM · c#variablejavaguiskin

Basic C# Public Variable Help

I'm using the component code, FileBrowser.cs, from the wiki page.

The problem is, is that I have just started C# and I know in java it is -

 var mySkin : GUISkin;

Also, My little knowledge of C# -

 public GUISkin mySkin;

My Main issue is that the code above does not show up in inspector as a "Public Variable" and that I've tried alot to fix it. But I cant seem to find the problem

Here Is My Real Code : Cited Variable at line 18!

 using UnityEngine;
 using System;
 using System.IO;
 using System.Collections.Generic;
  
 /*
     File browser for selecting files or folders at runtime.
  */
  
 public enum FileBrowserType {
     File,
     Directory
 }
 
  
 public class FileBrowser {
 
 public GUISkin GUI.skin;
  
     // Called when the user clicks cancel or select
     public delegate void FinishedCallback(string path);
     // Defaults to working directory
     public string CurrentDirectory {
         get {
             return m_currentDirectory;
         }
         set {
             SetNewDirectory(value);
             SwitchDirectoryNow();
         }
     }
     protected string m_currentDirectory;
     // Optional pattern for filtering selectable files/folders. See:
     // http://msdn.microsoft.com/en-us/library/wz42302f(v=VS.90).aspx
     // and
     // http://msdn.microsoft.com/en-us/library/6ff71z1w(v=VS.90).aspx
     public string SelectionPattern {
         get {
             return m_filePattern;
         }
         set {
             m_filePattern = value;
             ReadDirectoryContents();
         }
     }
     protected string m_filePattern;
  
     // Optional image for directories
     public Texture2D DirectoryImage {
         get {
             return m_directoryImage;
         }
         set {
             m_directoryImage = value;
             BuildContent();
         }
     }
     protected Texture2D m_directoryImage;
  
     // Optional image for files
     public Texture2D FileImage {
         get {
             return m_fileImage;
         }
         set {
             m_fileImage = value;
             BuildContent();
         }
     }
     protected Texture2D m_fileImage;
  
     // Browser type. Defaults to File, but can be set to Folder
     public FileBrowserType BrowserType {
         get {
             return m_browserType;
         }
         set {
             m_browserType = value;
             ReadDirectoryContents();
         }
     }
     protected FileBrowserType m_browserType;
     protected string m_newDirectory;
     protected string[] m_currentDirectoryParts;
  
     protected string[] m_files;
     protected GUIContent[] m_filesWithImages;
     protected int m_selectedFile;
  
     protected string[] m_nonMatchingFiles;
     protected GUIContent[] m_nonMatchingFilesWithImages;
     protected int m_selectedNonMatchingDirectory;
  
     protected string[] m_directories;
     protected GUIContent[] m_directoriesWithImages;
     protected int m_selectedDirectory;
  
     protected string[] m_nonMatchingDirectories;
     protected GUIContent[] m_nonMatchingDirectoriesWithImages;
  
     protected bool m_currentDirectoryMatches;
  
     protected GUIStyle CentredText {
         get {
             if (m_centredText == null) {
                 m_centredText = new GUIStyle(GUI.skin.label);
                 m_centredText.alignment = TextAnchor.MiddleLeft;
                 m_centredText.fixedHeight = GUI.skin.button.fixedHeight;
             }
             return m_centredText;
         }
     }
     protected GUIStyle m_centredText;
  
     protected string m_name;
     protected Rect m_screenRect;
  
     protected Vector2 m_scrollPosition;
  
     protected FinishedCallback m_callback;
  
     // Browsers need at least a rect, name and callback
     public FileBrowser(Rect screenRect, string name, FinishedCallback callback) {
         m_name = name;
         m_screenRect = screenRect;
         m_browserType = FileBrowserType.File;
         m_callback = callback;
         SetNewDirectory(Directory.GetCurrentDirectory());
         SwitchDirectoryNow();
     }
  
     protected void SetNewDirectory(string directory) {
         m_newDirectory = directory;
     }
  
     protected void SwitchDirectoryNow() {
         if (m_newDirectory == null || m_currentDirectory == m_newDirectory) {
             return;
         }
         m_currentDirectory = m_newDirectory;
         m_scrollPosition = Vector2.zero;
         m_selectedDirectory = m_selectedNonMatchingDirectory = m_selectedFile = -1;
         ReadDirectoryContents();
     }
  
     protected void ReadDirectoryContents() {
         if (m_currentDirectory == "/") {
             m_currentDirectoryParts = new string[] {""};
             m_currentDirectoryMatches = false;
         } else {
             m_currentDirectoryParts = m_currentDirectory.Split(Path.DirectorySeparatorChar);
             if (SelectionPattern != null) {
                 string[] generation = Directory.GetDirectories(
                     Path.GetDirectoryName(m_currentDirectory),
                     SelectionPattern
                 );
                 m_currentDirectoryMatches = Array.IndexOf(generation, m_currentDirectory) >= 0;
             } else {
                 m_currentDirectoryMatches = false;
             }
         }
  
         if (BrowserType == FileBrowserType.File || SelectionPattern == null) {
             m_directories = Directory.GetDirectories(m_currentDirectory);
             m_nonMatchingDirectories = new string[0];
         } else {
             m_directories = Directory.GetDirectories(m_currentDirectory, SelectionPattern);
             var nonMatchingDirectories = new List<string>();
             foreach (string directoryPath in Directory.GetDirectories(m_currentDirectory)) {
                 if (Array.IndexOf(m_directories, directoryPath) < 0) {
                     nonMatchingDirectories.Add(directoryPath);
                 }
             }
             m_nonMatchingDirectories = nonMatchingDirectories.ToArray();
             for (int i = 0; i < m_nonMatchingDirectories.Length; ++i) {
                 int lastSeparator = m_nonMatchingDirectories[i].LastIndexOf(Path.DirectorySeparatorChar);
                 m_nonMatchingDirectories[i] = m_nonMatchingDirectories[i].Substring(lastSeparator + 1);
             }
             Array.Sort(m_nonMatchingDirectories);
         }
  
         for (int i = 0; i < m_directories.Length; ++i) {
             m_directories[i] = m_directories[i].Substring(m_directories[i].LastIndexOf(Path.DirectorySeparatorChar) + 1);
         }
  
         if (BrowserType == FileBrowserType.Directory || SelectionPattern == null) {
             m_files = Directory.GetFiles(m_currentDirectory);
             m_nonMatchingFiles = new string[0];
         } else {
             m_files = Directory.GetFiles(m_currentDirectory, SelectionPattern);
             var nonMatchingFiles = new List<string>();
             foreach (string filePath in Directory.GetFiles(m_currentDirectory)) {
                 if (Array.IndexOf(m_files, filePath) < 0) {
                     nonMatchingFiles.Add(filePath);
                 }
             }
             m_nonMatchingFiles = nonMatchingFiles.ToArray();
             for (int i = 0; i < m_nonMatchingFiles.Length; ++i) {
                 m_nonMatchingFiles[i] = Path.GetFileName(m_nonMatchingFiles[i]);
             }
             Array.Sort(m_nonMatchingFiles);
         }
         for (int i = 0; i < m_files.Length; ++i) {
             m_files[i] = Path.GetFileName(m_files[i]);
         }
         Array.Sort(m_files);
         BuildContent();
         m_newDirectory = null;
     }
  
     protected void BuildContent() {
         m_directoriesWithImages = new GUIContent[m_directories.Length];
         for (int i = 0; i < m_directoriesWithImages.Length; ++i) {
             m_directoriesWithImages[i] = new GUIContent(m_directories[i], DirectoryImage);
         }
         m_nonMatchingDirectoriesWithImages = new GUIContent[m_nonMatchingDirectories.Length];
         for (int i = 0; i < m_nonMatchingDirectoriesWithImages.Length; ++i) {
             m_nonMatchingDirectoriesWithImages[i] = new GUIContent(m_nonMatchingDirectories[i], DirectoryImage);
         }
         m_filesWithImages = new GUIContent[m_files.Length];
         for (int i = 0; i < m_filesWithImages.Length; ++i) {
             m_filesWithImages[i] = new GUIContent(m_files[i], FileImage);
         }
         m_nonMatchingFilesWithImages = new GUIContent[m_nonMatchingFiles.Length];
         for (int i = 0; i < m_nonMatchingFilesWithImages.Length; ++i) {
             m_nonMatchingFilesWithImages[i] = new GUIContent(m_nonMatchingFiles[i], FileImage);
         }
     }
  
     public void OnGUI() {
         GUILayout.BeginArea(
             m_screenRect,
             m_name,
             GUI.skin.window
         );
             GUILayout.BeginHorizontal();
                 for (int parentIndex = 0; parentIndex < m_currentDirectoryParts.Length; ++parentIndex) {
                     if (parentIndex == m_currentDirectoryParts.Length - 1) {
                         GUILayout.Label(m_currentDirectoryParts[parentIndex], CentredText);
                     } else if (GUILayout.Button(m_currentDirectoryParts[parentIndex])) {
                         string parentDirectoryName = m_currentDirectory;
                         for (int i = m_currentDirectoryParts.Length - 1; i > parentIndex; --i) {
                             parentDirectoryName = Path.GetDirectoryName(parentDirectoryName);
                         }
                         SetNewDirectory(parentDirectoryName);
                     }
                 }
                 GUILayout.FlexibleSpace();
             GUILayout.EndHorizontal();
             m_scrollPosition = GUILayout.BeginScrollView(
                 m_scrollPosition,
                 false,
                 true,
                 GUI.skin.horizontalScrollbar,
                 GUI.skin.verticalScrollbar,
                 GUI.skin.box
             );
                 m_selectedDirectory = GUILayoutx.SelectionList(
                     m_selectedDirectory,
                     m_directoriesWithImages,
                     DirectoryDoubleClickCallback
                 );
                 if (m_selectedDirectory > -1) {
                     m_selectedFile = m_selectedNonMatchingDirectory = -1;
                 }
                 m_selectedNonMatchingDirectory = GUILayoutx.SelectionList(
                     m_selectedNonMatchingDirectory,
                     m_nonMatchingDirectoriesWithImages,
                     NonMatchingDirectoryDoubleClickCallback
                 );
                 if (m_selectedNonMatchingDirectory > -1) {
                     m_selectedDirectory = m_selectedFile = -1;
                 }
                 GUI.enabled = BrowserType == FileBrowserType.File;
                 m_selectedFile = GUILayoutx.SelectionList(
                     m_selectedFile,
                     m_filesWithImages,
                     FileDoubleClickCallback
                 );
                 GUI.enabled = true;
                 if (m_selectedFile > -1) {
                     m_selectedDirectory = m_selectedNonMatchingDirectory = -1;
                 }
                 GUI.enabled = false;
                 GUILayoutx.SelectionList(
                     -1,
                     m_nonMatchingFilesWithImages
                 );
                 GUI.enabled = true;
             GUILayout.EndScrollView();
             GUILayout.BeginHorizontal();
                 GUILayout.FlexibleSpace();
                 if (GUILayout.Button("Cancel", GUILayout.Width(50))) {
                     m_callback(null);
                 }
                 if (BrowserType == FileBrowserType.File) {
                     GUI.enabled = m_selectedFile > -1;
                 } else {
                     if (SelectionPattern == null) {
                         GUI.enabled = m_selectedDirectory > -1;
                     } else {
                         GUI.enabled =    m_selectedDirectory > -1 ||
                                         (
                                             m_currentDirectoryMatches &&
                                             m_selectedNonMatchingDirectory == -1 &&
                                             m_selectedFile == -1
                                         );
                     }
                 }
                 if (GUILayout.Button("Select", GUILayout.Width(50))) {
                     if (BrowserType == FileBrowserType.File) {
                         m_callback(Path.Combine(m_currentDirectory, m_files[m_selectedFile]));
                     } else {
                         if (m_selectedDirectory > -1) {
                             m_callback(Path.Combine(m_currentDirectory, m_directories[m_selectedDirectory]));
                         } else {
                             m_callback(m_currentDirectory);
                         }
                     }
                 }
                 GUI.enabled = true;
             GUILayout.EndHorizontal();
         GUILayout.EndArea();
  
         if (Event.current.type == EventType.Repaint) {
             SwitchDirectoryNow();
         }
     }
  
     protected void FileDoubleClickCallback(int i) {
         if (BrowserType == FileBrowserType.File) {
             m_callback(Path.Combine(m_currentDirectory, m_files[i]));
         }
     }
  
     protected void DirectoryDoubleClickCallback(int i) {
         SetNewDirectory(Path.Combine(m_currentDirectory, m_directories[i]));
     }
  
     protected void NonMatchingDirectoryDoubleClickCallback(int i) {
         SetNewDirectory(Path.Combine(m_currentDirectory, m_nonMatchingDirectories[i]));
     }
  
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by DaveA · Sep 05, 2012 at 11:30 PM

public GUISkin GUI.skin; // ??? maybe public GUISkin guiSkin instead

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Outlaw Lemur · Sep 05, 2012 at 11:31 PM 0
Share

Noooooooooooooo you beat me :)

avatar image
0

Answer by nickkunes · Sep 06, 2012 at 01:37 AM

Sadly, Guys, You need to assign the class to MonoBehaviour.

Like -

 public class FileBrowser : MonoBehaviour{
 
 }

But even after that, I still get a error, on line 239 D:

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by lodendsg · Sep 06, 2012 at 09:06 AM

As Outlaw noted above `public GUISkin GUI.skin;` wont play nice for you. the syntax for a C# variable decliration is

access specifor i.e. internal, private or public of which the defualt for objects declared outside a class i.e. classes, enums, etc. is internal while object declared within a class such as variables are private

next is the type i.e. String or in your case GUISkin

finaly is the name which may only contain letters and number as well as and must start with a letter or

Valid options(MyVar, _MyVar _MyVar1)

Invalid options(1MyVar, My.Var)

As noted your variable decliration should be 'public GUISkin GuiSkin' or similar.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to put the GUI into a gameobject so that when clicked, The GUI pops up, and heres a sample multiple Choice question using GUI, i need the code for it. 1 Answer

Multiple Cars not working 1 Answer

(C#) Public variables that have descriptions in the engine. 2 Answers

Distribute terrain in zones 3 Answers

C# adding ints 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges