Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Amodth · May 18, 2020 at 07:17 AM · androidiosnetworkingmultiplayer-networkingmirror

Get the device IP address from Unity

Hi,

I'm developing a multiplayer game to run on Android and IOS. I'm using Mirror Networking Library to handle the network related work. The game can be played between players who are connected to the same local network.

In my game there is no dedicated server instance and a player who hosts to game will act as the server. In that case I want to show the IP address of the player who hosts the game in the players screen so that others can manually enter the server's IP address and join the game.

I still couldn't come up with a solution to get the IP address of the local device through Unity or Mirror Networking Library. Is it possible? If so how to get it.

This game is intended to run on Android and IOS. So the solution should support both the platforms.

Thanks.

Comment
Add comment · Show 1
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 MuhammadKalimullah · Apr 05 at 08:20 PM 0
Share

Hello, Amodth Can you please explain how did you convert your project from localhost to LAN?

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by GetLitGames · May 18, 2020 at 08:41 AM

     public string GetLocalIPv4()
     {
         return Dns.GetHostEntry(Dns.GetHostName())
             .AddressList.First(
                 f => f.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
             .ToString();
     }    
 
Comment
Add comment · Show 3 · 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 Bunny83 · May 18, 2020 at 10:15 AM 0
Share

Note that just grabbing the first entry usually works well for PCs but does not necessarily work that well on mobile. Android usually favors IPv6. Also Android devices are often connected to several networks at the same time and could have several different addresses. However this should get him on the right track ^^.

avatar image Amodth · Sep 04, 2020 at 05:03 AM 0
Share

Hi SubtleTechnology Yes this works. But the problem is as Bunny83 mentioned it returns the 1st IP address from the list. What I actually want is to get is the WiFi IP address of the device. Is there a way to specifically return the Wifi IP address instead of the 1st IP from the list?

avatar image Bunny83 Amodth · Sep 04, 2020 at 03:51 PM 0
Share

There is nothing like a dedicated WiFi IP range. Just by looking at the addresses you can'r really tell what this address belongs to. You can rule out some addresses like the local loopback (127.x.x.x) and maybe some others. However WiFi could use pretty much any address range. Usually WiFi would use addresses from the private address range. Keep in $$anonymous$$d that IPv4 has different private ranges then IPv6.


There are other platform dependent solutions to get the actual WiFi information from the OS. Of course on mobile devices this most likely requires additional privileges and some native code plugin.

avatar image
0

Answer by AleixT · Jul 16, 2020 at 02:47 PM

Hi Amodth, I'm trying to do the same, connect different devices in the same room by the IP address. Have you do it successfully?

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 Honorsoft · Oct 31, 2020 at 07:56 AM

I am also starting out with using Mirror, but I have some info that might help... you can get the IP's and see what players (servers or sever-clients) are available by adding the "Network Discovery" and "Network Discovery HUD" components included with Mirror. Any hosted games (servers/server-clients) should show up in the list.

I am also running some more tests on Mirror by adding some 3rd-party code for IP detection. I am not sure if the IPv4 address is the one needed to connect Mirror on mobile devices, but here's some Unity code to get the IPv4 (un-tested by me so far):

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using System.Net;
 using System.Net.NetworkInformation;
 using System.Net.Sockets;
 
 public class TestLocationService : MonoBehaviour
 {
     public Text hintText;
 
     private void Start()
     {
         GetLocalIPAddress();
     }
     public string GetLocalIPAddress()
     {
         var host = Dns.GetHostEntry(Dns.GetHostName());
         foreach (var ip in host.AddressList)
         {
             if (ip.AddressFamily == AddressFamily.InterNetwork)
             {
                 hintText.text = ip.ToString();
                 return ip.ToString();
             }
         }
         throw new System.Exception("No network adapters with an IPv4 address in the system!");
     }
 
 }

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

339 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Network Manager for Android 1 Answer

Application.genuine 0 Answers

Mirror: Can't connect to Oculus Quest 2 (Android) Host - Instant Disconnect 1 Answer

Cross platform multiplayer 3 Answers

Does UnityEngine.Ping currently support ipv6 addresses on mobile devices? 0 Answers


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