- Home /
Link UI Input Field to Script
The Script in question is this one:`using UnityEngine; using System.Collections;
public class GoogleMap : MonoBehaviour { public enum MapType %|-1957000663_2|% RoadMap, Satellite, Terrain, Hybrid %|-632995662_7|% public bool loadOnStart = true; %|-1464234556_9|% public GoogleMapLocation centerLocation; public int zoom = 13; %|-1414845350_12|% public int size = 512; public bool doubleResolution = false; public GoogleMapMarker[] markers; public GoogleMapPath[] paths;
%|1077180128_17|% { if (loadOnStart) Refresh(); %|1841942021_20|%
public void Refresh()
{
if (autoLocateCenter && (markers.Length == 0 && paths.Length == 0))
%|-122252710_24|% %|-1704368595_25|% } %|-2091729669_27|% }
%|-1002132817_29|% { %|1805603506_31|% %|1791570299_32|% %|-777112843_33|% %|-1967131424_34|% %|-1189092094_35|% %|543796066_36|% else { qs += "center=" + WWW.UnEscapeURL(string.Format("{0},{1}", centerLocation.latitude, centerLocation.longitude)); }
qs += "&zoom=" + zoom.ToString();
}
qs += "&size=" + WWW.UnEscapeURL(string.Format("{0}x{0}", size));
qs += "&scale=" + (doubleResolution ? "2" : "1");
qs += "&maptype=" + mapType.ToString().ToLower();
var usingSensor = false;
if UNITY_IPHONE
usingSensor = Input.location.isEnabledByUser && Input.location.status == LocationServiceStatus.Running;
endif
|2136518741_48|%
foreach (var i in markers)
{
qs += "&markers=" + string.Format("size:{0}|color:{1}|label:{2}", i.size.ToString().ToLower(), i.color, i.label);
foreach (var loc in i.locations)
%|-1310872866_53|% if (loc.address != "") %|-1553893620_55|% %|966862394_56|% %|2012631069_57|% %|-1709193653_58|% }
%|-126405656_60|% { %|202957784_62|% %|1969094223_63|% %|-595190589_64|% { if (loc.address != "") qs += "|" + WWW.UnEscapeURL(loc.address); else %|-1988163170_69|% %|-1309563583_70|% %|30247691_71|%
var req = new WWW(url + "?" + qs);
%|1900091126_73|% %|697756430_74|% %|-738730212_75|%
}
public enum GoogleMapColor { black, %|-1366980874_77|% %|-482055578_78|% purple, yellow, %|137787309_81|% gray, orange, %|-1282336543_84|% white }
[System.Serializable] public class GoogleMapLocation { %|-1729647901_86|% %|538392101_87|% public float longitude; }
[System.Serializable] public class GoogleMapMarker { public enum GoogleMapMarkerSize { Tiny, %|-244782460_92|% Mid } public GoogleMapMarkerSize size; public GoogleMapColor color; %|614298737_97|% %|-552143230_98|%
}
[System.Serializable] public class GoogleMapPath { public int weight = 5; public GoogleMapColor color; public bool fill = false; public GoogleMapColor fillColor; public GoogleMapLocation[] locations; }
What I want to do is provide an Input Field that will allow the user to enter the Latitude and Longitude values for Map Center in a UI Input field.
So far nothing I have tried does what is desired. I want the user to be able to enter and update map after filling in Latitude and Longitude values.`
Are you able to give JUST the base code without formatting changes?
Answer by Creeper_Math · Jan 04, 2017 at 08:54 PM
This requires adding a reference to two UI text fields (which should be set to Int input). Below is the script that would work
// Add to using
using UnityEngine.UI;
// Add to variables
public InputField Longitude;
public InputField Latitude;
// Add this to the Start function if u have any
Longitude.onValueChanged.AddListener (delegate {UpdateLocation();});
Latitude.onValueChanged.AddListener (delegate {UpdateLocation();});
// Add this function for updating the variables
void UpdateLocation() {
float.TryParse(Longitude.text, out CenterLocation.Longitude);
float.TryParse(Longitude.text, out CenterLocation.Latitude);
}
Answer by mfarrell806 · Jan 04, 2017 at 10:34 PM
Thanks for the above, easy enough to figure out where the USING stuff goes,
However being new and confused...not sure where I would put the stuff for Variables, as the only ones I see and recognize are in the CoRoutine _Refresh
So where would I insert the other portions of the code provided?
It's interesting to note that I have seen others post similar question; and no one other than yourself has provide any usable answer.
So you will be helping quite a few with these answers,
Thanks in advance!
The Variables would go right after the "$$anonymous$$onobehavior" script, so in your example, they would go with public class Google$$anonymous$$ap : $$anonymous$$onoBehaviour {
The second set of code would go under a new void (as you don't have a Start() ). Since you don't have one, then just add this into your code at any part inside the monobehavior, as well as the update locations.. Put the following snippet as a new void
void Start() {
Longitude.onValueChanged.AddListener (delegate {UpdateLocation();});
Latitude.onValueChanged.AddListener (delegate {UpdateLocation();});
}
void UpdateLocation() {
float.TryParse(Longitude.text, out CenterLocation.Longitude);
float.TryParse(Longitude.text, out CenterLocation.Latitude);
}
Also, for further unity answers, for this type of response, please put it as a "comment" under my answer ins$$anonymous$$d of a separate answer
There is a start, although it looks like I don't due to how the website posted my code.
Let me try again...meanwhile I see what happens if I put those lines within the current start statement.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Google$$anonymous$$ap : $$anonymous$$onoBehaviour
{
public enum $$anonymous$$apType
{
Road$$anonymous$$ap,
Satellite,
Terrain,
Hybrid
}
public bool loadOnStart = true;
public bool autoLocateCenter = true;
public Google$$anonymous$$apLocation centerLocation;
public int zoom = 13;
public $$anonymous$$apType mapType;
public int size = 512;
public bool doubleResolution = false;
public Google$$anonymous$$ap$$anonymous$$arker[] markers;
public Google$$anonymous$$apPath[] paths;
public InputField Longitude;
public InputField Latitude;
void Start()
{
if (loadOnStart) Refresh();
}
public void Refresh()
{
if (autoLocateCenter && (markers.Length == 0 && paths.Length == 0))
{
Debug.LogError("Auto Center will only work if paths or markers are used.");
}
Obviously where I inserted them is the wrong place or method, Upon adding those lines within Start() function I now get this error
Google$$anonymous$$ap.Start () (at Assets/Standard Assets/Scripts/Google$$anonymous$$ap.cs:31)NullReferenceException: Object reference not set to an instance of an object
i think you should check: markers.Length == 0 && paths.Length == 0
O$$anonymous$$, what would I be checking for?
I ask out of ignorance. Because the script worked before, although I really wanted to give the user input fields to enter their actual location or any global location during game play. Thus my request for the ability to properly link two input fields to the Latitude and Longitude that the Google $$anonymous$$ap script uses.
So, what would I set them to, and if you know where would I place the
Longitude.onValueChanged.AddListener (delegate {UpdateLocation();});
Latitude.onValueChanged.AddListener (delegate {UpdateLocation();});
}
void UpdateLocation() {
float.TryParse(Longitude.text, out CenterLocation.Longitude);
float.TryParse(Longitude.text, out CenterLocation.Latitude);
}
Within the script that I have?
Thanks!
Your answer
Follow this Question
Related Questions
How do I disable player input action map and enable the UI action map? 4 Answers
How to change a float value with UI buttons,How Can I control a character with UI buttons? 1 Answer
Optimal way to display a player's health with hearths? 1 Answer
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers