• 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 jaramillo · Jan 02, 2012 at 08:42 PM · windowsmacsystemenvironmentusername

Get current username

Hi, i'm trying to get the current username on Windows and Mac.

For Mac i found Environment.UserName but it returned NULL

Can anyone help me to do this on windows and Mac using Javascript code?

Thanks.

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
3

Answer by dannyskim · Jan 02, 2012 at 09:23 PM

Environment.Username is the appropriate call, it works perfectly fine for me inside the editor.

I'm making an assumption here, but have you made sure you included the import System; at the top of your javascript file?

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 jaramillo · Jan 02, 2012 at 09:34 PM 0
Share

Sorry i answered you in another in the next posted answer =P Thanks.

avatar image
1

Answer by cbordeman_unity · May 18, 2019 at 12:42 PM

The current user can mean many things. If you just want the profile logged into the device,

 string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

This gets the user the game is running as, which is safer than Environment.Username.

If your game is using OAuth or its own internal login system, then that won't work.

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 shadow7412 · Feb 01 at 11:49 AM 0
Share

It should be fairly obvious by the name, but that API isn't cross-platform. Use it with care if you want your game to compile on non-windows machines.

avatar image
0

Answer by jaramillo · Jan 02, 2012 at 09:33 PM

Yes, i did. i don't know why it throws NULL but i already solved it. Thanks ;)

If anyone has this problem here's the solution.

System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) returns the current username on Windows and Mac

Comment
Add comment · Show 2 · 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 cbordeman_unity · May 18, 2019 at 12:33 PM 1
Share

LOL no, it doesn't.

avatar image hacky97 · Apr 02, 2020 at 08:01 AM 0
Share

@cbordeman_unity , This post from more than 7 years ago that you reacted to is not something to LOL about.
This could potentially return the folder c:\users\\Documents. With some fiddling you can extract the username. Not very elegant, but it might be a solution if all else fails.

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

8 People are following this question.

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

Related Questions

"Out of Memory" on Windows Build, Runs perfectly on Mac Build 1 Answer

Mac VS Windows Builds 3 Answers

How to load Windows licence into Mac? 1 Answer

Unity porting (windows, linux, ect) 0 Answers

i found this login system how would i get it to work? 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